Bottom Type--Type Theory

Johannes Loher johannesloher at fg4f.de
Thu Jan 17 05:04:38 UTC 2019


Am 17.01.19 um 03:59 schrieb Meta:
> D has a couple of different unit types which all behave differently:
> 
> typeof(null), its sole value being null. It also happens to be the
> bottom type for all objects, pointers and arrays.
> 
> 
> void, which actually *does* have a single value - you just can't declare
> a variable with type void:
> 
> void main()
> {
>     auto v = new void[1];
>     pragma(msg, typeof(v[0])); // Prints "void"
>     import std.stdio;
>     writeln(v[0]); // Error: template std.stdio.writeln cannot deduce
> function from argument types !()(void)
> }
> 
> This may actually be a compiler bug/quirk; I'm not completely sure.
> 
> 
> Any enum with only 1 element:
> 
> enum Unit
> {
>     val
> }
> 

I have already resorted to using `struct Unit {}` as a proper unit type
in the past. The problem with this however is that it is only
convention. As soon as you have to interface with somebody else's code,
you lost, because they all use void and expect you to use void.

> As an aside, the empty enum:
> 
> enum Empty
> {
> }
> 
> Is an uninhabited type similar to Bottom, but is not implicitly
> convertible to any other type, unlike Bottom.
> 
> 
> The empty struct:
> 
> struct Unit
> {
> }
> 
> Unit u;
> writeln(u); // Prints Unit()
> 
> 
> And probably more that I have missed

While all of this is true, in my opinion there should be one "canonical"
unit type in the sense that it works as a proper unit type (it has only
one value, you can declare variables of it) and it is implicitly
returned from functions which do not have an explicit return statement
(and are not inferred to return Tbottom, if that is added). The
candidate for such a type would be void, but there are quite few things
we'd need to fix as H. S. Theo explained somewhere in this thread.


More information about the Digitalmars-d mailing list