Value-Based Classes

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


 Hi!

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].)

 Greetings
 Nicolai Parlog


[1]
https://docs.oracle.com/javase/8/docs/api/java/lang/doc-files/ValueBased.html
[2] http://docs.oracle.com/javase/8/docs/api/java/time/LocalTime.html
[3] https://github.com/CodeFX-org/FindBugs-Fork/pull/1



-- 

PGP Key:
    http://keys.gnupg.net/pks/lookup?op=vindex&search=0xCA3BAD2E9CCCD509

Web:
    http://codefx.org
        a blog about software development
    http://do-foss.de
        Free and Open Source Software for the City of Dortmund

Twitter:
    https://twitter.com/nipafx



More information about the valhalla-dev mailing list