The NaN of types (in D)

Stefan Koch uplink.coder at googlemail.com
Sun Oct 11 12:18:59 UTC 2020


Good Morning,

today I would like to write about something which came up while 
implementing type functions.

It turns out that it is useful, to have a .init value for __type.
The language expects it, phobos' std.algorithm breaks in horrible 
ways when this is violated.
So that poses the question, what should this value be.
It cannot really be a valid type.
One option would be to use the internal error type called Terror.
It turns out that type is aptly named.
Using it will make every mention of the __type.init value an 
error.
We do not want this.
It's fine to return the init value as long as is(type.init) does 
return false.

So we need a type that behaves just like Terror (i.e. is not 
considered a type by the is expression) but is not an error.

This type is the empty type (Ø).
It is emptier than void and on the same level as bottom (⊥).
But Ø cannot be ⊥, because ⊥ can be a valid return type of a 
function.
(At least once we have the ⊥ type)

Ø not being a valid type as far as the is expression is concerned.
(is(Ø) yields false)
Also means that is(Ø == Ø) also yields false.

And this is where the similarity to NaN comes from, which allowed 
me to give the post this fancy title.

Please ask me anything, about Ø or type functions.
I am happy to answer questions, and maybe we find more 
contradictions that we can resolve.

Cheers,

Stefan


More information about the Digitalmars-d mailing list