4 ms·
One downside of Java records seems to be that they can't participate as the alternatives (subtypes) for a sealed abstract type, since they can't extend any clas
by sixbrx 4y ago
One downside of Java records seems to be that they can't participate as the alternatives (subtypes) for a sealed abstract type, since they can't extend any classes other than Object. Ie. the very common "sum of product types" pattern can't use records as the product types - which they would have been great for! You can have normal wrapper classes as the subtypes of the sealed type, and have those wrappers use records internally, but all the brevity and clarity is lost that way IMO and it just becomes obfuscation.
- pron 4y agoOf course they can! They can implement interfaces, which are sealed. In fact, that's one of their most common usages: sealed interface Option { record InputFile(Path path) implements Option {} record OutputFile(Path path) implements Option {} record MaxLines(int maxLines) implements Option {} record PrintLineNumbers() implements Option {} } (see https://www.infoq.com/articles/data-oriented-programming-java/ https://www.infoq.com/articles/data-oriented-programming-jav...) The problem of extending classes is that it can introduce hidden state and also result in non-reflexive equality. But interfaces are fine.
- sixbrx 4y agoThanks for that, I knew I must have been missing something!