Nullable or Optional? Or something else?
Steven Schveighoffer
schveiguy at yahoo.com
Thu Sep 3 08:19:08 PDT 2009
On Thu, 03 Sep 2009 02:36:12 -0400, Rainer Deyke <rainerd at eldwood.com>
wrote:
> Andrei Alexandrescu wrote:
>> Rainer Deyke wrote:
>>> You need some syntactic way to distinguish the contained value from the
>>> container. Using "alias this" seems messy here. Optional!Optional!T
>>> is
>>> both valid and likely to occur.
>>
>> Interesting. I wonder whether it's better to fold Optional!(Optional!T)
>> into Optional!T.
>
> That would be semantically incorrect. Optional!T must be able to hold
> all possible values of T, plus a distinct null value.
>
> Consider:
>
> Optional!int upper_limit;
>
> Optional!(Optional!int) config_limit = config_file.get("upper_limit");
> if (isNull(config_limit)) {
> upper_limit = default_upper_limit;
> } else {
> upper_limit = config_limit; // Could be null.
> }
>
> Then consider that this could be in a template function, with
> Optional!int supplied as a template argument.
How does one distinguish an Optional!(Optional!int) that is null from an
Optional!(Optional!int) whose Optional!int is null? If there's no way to
test for it, then they are not conceivably different.
I think Andrei is right here, for any type T that already is nullable
(including pointer and reference types), Optional!T should alias to T.
-Steve
More information about the Digitalmars-d
mailing list