Sum Types - first draft

Paul Backus snarwin at gmail.com
Tue Sep 10 17:23:04 UTC 2024


On Tuesday, 10 September 2024 at 17:05:49 UTC, Walter Bright 
wrote:
> Thanks for your detailed response. Let me address just one for 
> the moment:
>
> On 9/10/2024 9:20 AM, Paul Backus wrote:
>>> * std.sumtype cannot optimize the tag out of existence, for 
>>> example, when having:
>>>
>>>       enum Option { None, int* Ptr }
>> 
>> A built-in sum type would not be able to do this either, 
>> because in D, every possible sequence of 4 bytes is a 
>> potentially-valid int* value.
>> 
>> The reason Rust is able to perform this optimization is that 
>> Rust has non-nullable reference types [2]. If D had 
>> non-nullable pointer types, then std.sumtype could perform the 
>> same optimization using reflection and `static if`.
>
> I was approaching it from the other way around. Isn't a 
> non-nullable pointer a sumtype? Why have both non-nullable 
> types and sumtypes?

You have it exactly backwards. A _nullable_ pointer type is the 
sum of a non-nullable pointer type and typeof(null).

A non-nullable pointer type is a pointer type with its range of 
valid values restricted. You could think of it as a "difference 
type"--if you take T*, and _subtract_ typeof(null) from it (i.e., 
take the set difference [1] of their values), you get a 
non-nullable pointer type.

[1] 
https://en.wikipedia.org/wiki/Complement_(set_theory)#Relative_complement


More information about the dip.development mailing list