|
|
|
|
|
by akavi
637 days ago
|
|
You can get strongly typed "shaped" data without objects[0], even in Java: Records[1]. ~Unfortunately, I believe they're mutable (and cannot be made immutable).~ Edit: I was wrong, they're immutable. [0]: I'm using "object" to mean "data bound to methods", since the concept of aggregate data in general long pre-date OOP (eg, C's structs) [1]: https://docs.oracle.com/en/java/javase/17/language/records.h... |
|
The frustration I have with Records is there is no good way to prevent direct construction of them. That is, the constructor is public, which prevents an easy way of enforcing an invariant during construction.
For example, let's say that you have a record with a Date type. There's no good way to prevent a user from creating the record with an invalid date, one that is out of a needed date range. Or maybe enforcing a field cannot be null or some combination of fields must meet requirements as a group.
The benefit I get from the classic Builder pattern is defeated with Records. I can't enforce checking of my fields before the construction of the record object itself. Presumably I would need to verify the object after construction, which is unfortunate.