more band aid coming - Object methods on 'any' tvars

MacGregor, Duncan (GE Energy Management) duncan.macgregor at
Thu Jan 15 11:19:16 UTC 2015

On 15/01/2015 11:14, "Maurizio Cimadamore"
<maurizio.cimadamore at> wrote:
>On 15/01/15 11:09, MacGregor, Duncan (GE Energy Management) wrote:
>>Part of implementing my collection involved making a suitable
>> T> style type, and this turns out to be slightly more fiddly than I
>> have hoped as there is no way to specify a default value for any val.
>There might be some updates soon in that department :-)


>> Java.util.Optional<T> defines EMPTY as an optional with its final field
>> set to null, but I can¹t do that across any T. I¹ve worked round the
>> problem by using two subclasses, Empty<T> (which doesn¹t have a value
>> field), and Present<T> (which does and always initialises it), but this
>> only works because my Optional is a reference type, and I don¹t think we
>> really want Optional type things to be ref types in the long run.
>I think some of the proposals I've seen on the table were about using an
>extra boolean in order to capture the 'is present' info.

That is how I was intending to implement it, but if I don’t want to
subclass then I need have a type something like

Class Optional<any T> {
	final T value;
	final boolean hasValue;

And that means I need to set value to something, even for the empty case.
Hence the need for defaults.


More information about the valhalla-dev mailing list