What is the rationale behind enabling Nullable!T to be used as a regular T?

aliak something at something.com
Fri Feb 14 21:52:24 UTC 2020


On Friday, 14 February 2020 at 17:04:28 UTC, Adnan wrote:
> On Friday, 14 February 2020 at 14:43:22 UTC, Petar Kirov 
> [ZombineDev] wrote:
>> On Friday, 14 February 2020 at 08:54:41 UTC, Adnan wrote:
>> The two ends of the design spectrum (as I see it) are:
>> 1. `Optional(T) x` as a range of [0,1] elements of type `T`. 
>> For all possible operations `op` on type `T` there exists an 
>> operation `Option(T).op` and the effect of executing is 
>> equivalent to `x.map!fun`. This is what aliak's optional 
>> package provides.
>
> Very interesting. I always thought that Option<T> is a 
> type-guard for enforced null checks (I could be wrong here). 
> But seems to me that this design circles back to square 1: 
> having the callee remember to check if the length of the range 
> is 0 or 1. Which is essentially similar to check sentinel 
> values (i.e. check if the binarysearch returns -1 as index). 
> What languages do this?

Ya! If you take scala for e.g. Optional!T is an attempt to model 
a lot of what scala does. I.e. it's a range. If you want to get 
to the element you have to check if it exists first, else you 
manipulate it via functional primitives like map. You need to do 
the same in scala, kotlin, swift for e.g.

I used to have an unwrap function in there as well, and you could 
do:

if (auto v = optional.unwrap) {
   // you get a pointer to T here and in D you can access members 
of a pointer
   // just like it wasn't a pointer.
}

But that proved to be hard to keep @safe.

I've never been a 100% on the whole operator overloading though. 
The difference between how it's implemented here and other places 
is that it's a noop if the optional is empty.

In swift you can call functions on optionals even if they're 
empty, so that's what the oc utility in optional is for, and the 
opBinary was a way to add that same functionality for D operators 
and array access.

Basically, operating on an optional is ok and always results in 
an optional. And it must always maintain it's emptiness if any of 
the operands are empty.

>
> What does Aliak's package provide that's fundamentally 
> different to just returning a T[]? Empty T[] would mean `None` 
> and a T[] with 1 item means `Some(T)`?

I've tried to outline as much as possible in the readme. In a 
nutshell though: intent, @nogc, safe access, no need to worry 
about having more than one element - I may be forgetting some 
things.

Cheers,
- ali


More information about the Digitalmars-d mailing list