sentinels & who owns the value space Re: valhalla-dev Digest, Vol 17, Issue 10
twhitmore.nz at gmail.com
Thu Dec 24 23:35:32 UTC 2015
I've tended to approach "type" as something generic specialization should
abstract us above. Mentally I believe it would be best to get us much
mileage as possible out of abstraction -- being able to code today's
idioms, but abstracted above the level of null/ reftypes/ primitives to
generic understandings of what these are being used for.
T.default provides an abstracted generic "default value";
- zero or null, etc.
- for Value Types, 'default' answers a value with all fields set to
T.sentinel similarly would provide an abstracted generic "sentinel value';
- as distinct as reasonably possible, from normally-used values of that
- for integrals, it would eg. Integer.MIN_VALUE, Byte.MIN_VALUE;
- for floats, perhaps NaN or minus NaN;
- for boolean, there is no ideal sentinel; but if we use 'true',
algorithms such as Moh's will still operate correctly.
- for Value Types, 'sentinel' answers a value with all fields set to
Thus far we have defined T.default and T.sentinel as distinct values, for
all primitive types & value types composed of primitives, in such a way
that "open addressed" hash structures (Moh's example" are able to represent
both "empty" and "removed" cells.
At this point the approach has not answered how to provide a distinct
sentinel for ref-types (other than 'null').
However being able to code Moh's algorithm in the abstract -- no need for
specialized code -- such that it works over all primitive types & all value
types composed of primitive types, is immensely useful. This enables us to
cover vast amounts of the problem space/ type universe --
With just a single class & two 10-character syntaxes, providing abstract
Sentinels are something that has real mileage at the concrete level, and
thus would be an omission not to be "abstract over" in code at the generic
The obvious further question, would be whether it is possible to provide a
distinct sentinel for ref-types other than 'null'. The expression would
have to be efficient & usable for all possible reftypes. There are perhaps
a couple of approaches:
1) similar to today's "weakly typed" reftype generics, use a single
Object SENTINEL global constant & defer type-checking;
2) factor efficient 'sentinel' and 'default' type-support into some
TypeFactory, referenced by the collection.
3) (probably theoretical only) define a "bottom type" with one concrete
value. Special-case would be needed in the JVM to handle it. See:
More information about the valhalla-dev