4 ms·
I don’t believe this is true. From my reading of the document, all with expressions go through the primary constructor.
by wtetzner 4y ago
I don’t believe this is true. From my reading of the document, all with expressions go through the primary constructor.
- ivan_gammel 4y agoWell, that is exactly the problem, as I already explained a few times in this thread. Constructor cannot validate state transitions, so the “with” statement or the block surrounding it will have to do it, breaking encapsulation and likely requiring multiple copies of this code.
- wtetzner 4y agoBut if that’s the case, then a record is the wrong data type for what you’re doing anyway, since you can call the primary constructor at any point with those same un-validatable values. It’s not “with” that’s problematic. If a record doesn’t work for what you’re trying to do, just use a class. Though I will say that this is why I generally think ML’s (i.e. OCaml, Standard ML) approach to encapsulation with modules is generally superior to using classes for encapsulation.
- ivan_gammel 4y agoHave a look at the proposal. Half of it is dedicated to regular classes, which makes sense, because record is just a syntactic sugar in Java at the moment. Also this: https://news.ycombinator.com/item?id=34398982 https://news.ycombinator.com/item?id=34398982