valhalla-dev Digest, Vol 7, Issue 24
Thomas W
twhitmore.nz at gmail.com
Thu Jan 8 01:17:37 UTC 2015
Hi Vitaly, people,
> So i thought having equality externalized means you don't deal with it in
the generic class - you call a black box passing a default and target item,
and get an answer.
That's fine as a possible *implementation mechanism*, but it doesn't help
the language syntax. Nor does it expose so much to the Hotspot optimizer.
It's a reasonable idea and I'm happy to consider it, though :)
> Now yes the default for ref types is null but for better or worse null
isn't going away in java so pretending it doesn't exist is just silly.
Null absolutely exists in reftypes, but it absolutely does not for
primitives or value-types. We should be recognizing the /logical meaning/
of eg Collections code that writes 'null', and provide a meaningful
expression that applies across both reftypes and value-types.
I did previously note that nulls are a huge pain-point for application
developers in Java. So it seems very unlikely that I am pretending they
don't exist! I want to address null at a language level for the Equals
operator, so that we have a "universal equals" that works at a logical
level -- across nullable/ and valuetypes equally. This would be hugely
beneficial to application development, in a way that IEquatable/
IEqualityComparer would not.
A quick outline of what 'null' is typically used for in Collections-type
code may help illustrate my point:
1) elements[i] = null;
// clearing or defaulting; --> T.empty aka T.default.
2) if (cand != null && cand.equals(item))
// 'eq' operator; --> cand eq item
3) if (node == null) return null;
// not found returning sentinel; --> if (node == null) return
T.sentinel
// of course, we should suggest to use getOrDefault() or
getOptional() if a sentinel is unsuitable -- for either reftypes or
value-types!
I can be quite clear, this very fairly recognizes & supports 'null' in
reftypes. But very importantly, it avoids requiring or encouraging 'null'
to be written in such code -- by providing meaningful alternatives, which
are meaningful for primitives/ valuetypes as well as for reftypes.
Animal.speak(), rather than Duck.woof().
Regards,
Thomas
More information about the valhalla-dev
mailing list