Bottom Type--Type Theory
H. S. Teoh
hsteoh at quickfur.ath.cx
Thu Jan 17 22:21:45 UTC 2019
On Thu, Jan 17, 2019 at 09:29:53PM +0000, Neia Neutuladh via Digitalmars-d wrote:
> On Thu, 17 Jan 2019 21:48:52 +0100, Johannes Loher wrote:
> > I don't actually think declaring a variable of the Bottom type is
> > required to be illegal. It just needs to be impossible for it to
> > ever be initialized.
>
> Which runs into default-initialization, so that's not awesome. But the
> same is true of structs with `@disable this();`
>
> If we treated them the same, you'd still be able to write:
>
> TBottom var = void;
> doStuff(var);
>
> That's...not awesome.
It looks that way, but actually it totally makes sense. It only looks
insane because we're thinking of it as a standalone, explicit function.
But it's a lot less insane if you think of generic code that take
arbitrary type parameters, e.g.:
void func(T)(bool choice)
{
if (choice)
{
T var;
doStuff(var);
}
}
In this case, it's an asymmetry if func doesn't compile when T ==
Bottom. The writer of func would have to use sig constraints or static
if to specifically check for Bottom.
If instead we allow the declaration of `Bottom var;`, with the
stipulation that it corresponds with the runtime code for halting the
program (e.g., assert(0)), then the implementor of func wouldn't even
have to think about the possibility of T == Bottom. When the user
passes Bottom to func, the function naturally just aborts where var is
declared.
This then allows func to be used both for invoking doStuff if `choice`
is true, and also, by passing T == Bottom, to *abort* the program if
`choice` is true. This becomes fully transparent to the implementor of
`func` -- you wouldn't need to special-case Bottom, and you wouldn't
need to write another function specifically made to abort the program
when choice == true. The same code would work for all cases.
T
--
Why can't you just be a nonconformist like everyone else? -- YHL
More information about the Digitalmars-d
mailing list