Value-Based Classes

Nicolai Parlog nipa at
Sat Feb 14 09:37:21 UTC 2015


I have two questions regarding value-based classes[1]. I considered
posting them to other lists (lambda-dev?) but thought that their
relation to value types makes this list a good place to ask them. If
not, I will happily post them elsewhere.

#1: Serialization

[N.B.: I'm a little fearful bringing up serialization, knowing how
much of a favorite feature it is for some of the people on this list.]

How come value-based classes are not allowed to be used for
serialization (being an "identity sensitive operation") but still many
such classes are serializable (e.g. [2])? I can understand both sides,
i.e. why the limitation is necessary and why value-based classes
should "of course" be serializable.

Is there any insight into that tension or into plans on how to break it?

#2: Implementation of equals, hashCode, toString

How am I to understand the following sentence?

"[Instances of a value-based class] have implementations of equals,
hashCode, and toString which are computed solely from the instance's
state and not from its identity or the state of any other object or
variable;" (from [1])

What exactly is "any other object"? Would, e.g., the value wrapped by
an Optional count as one? If so, isn't calling equals on that value a
computation based on its state?

Besides general curiosity I am interested in this because I will soon
be working on FindBugs rules which check these declaration site
limitations and warn the user if they overstep them in their own
value-based classes. (I will start after being done with enforcing the
use site limitations[3].)

 Nicolai Parlog



PGP Key:

        a blog about software development
        Free and Open Source Software for the City of Dortmund


More information about the valhalla-dev mailing list