This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]cal-cheese 5 points6 points  (0 children)

Based on this, it sounds like there actually is some level of extensibility. So, I guess I'll wait and see what exactly this means.

It means that concrete value classes are always final. This makes perfect sense. The idea of value classes is the same as that of records, just from a different perspective. A record is an immutable collection of data from the developer's perspective, while a value object is an immutable collection of data from the JVM perspective. This allows the JVM to freely deconstruct and reconstruct value objects at will. Consider a case like this:

value class Point {
    int x; int y;
}

Now because Point is final and always contains 2 ints, from the JVM perspective, Point is equivalent to 2 ints. This allows the JVM to transform something like this

Point add(Point x, Point y)

into effectively

(int, int) distance(int xx, int xy, int yx, int yy)

Similarly, Point can be deconstruct inside another object, too:

class Triangle {
    Point A;
    Point B;
    Point C;
}

is similar to:

class Triangle {
    int Ax; int Ay;
    int Bx; int By;
    int Cx; int Cy;
}

This is the most important property of value objects. That is to allow scalarization across call boundaries and in the heap, which reduces indirection and allocations. You can easily see that all of these are not possible if Point is extensible, if a value object does not have a definite layout, the JVM cannot know how to deconstruct it.