Nullable or Optional? Or something else?

Rainer Deyke rainerd at eldwood.com
Fri Sep 4 19:45:40 PDT 2009


Steven Schveighoffer wrote:
> I disagree, how many ways do you want to say something is unset?  I
> think one case -- is this thing null -- is plenty for 99% of cases. 
> Your case, well, I had trouble understanding it, but I think the nuance
> of how something is unset is not usually important.

Possible use case for Optional: storing an upper limit.  In this case
"null" means "there is no upper limit".

Possible use case for Optional: a function that retrieves a value from a
configuration file.  In this case, "null" means "the value is not found
in the configuration file".

So what happens if you want to store that there is no upper limit in a
configuration file?


> I think that what you will find with a solution that *does* allow this,
> you will be sacrificing something else -- such as syntax clarity or a
> member function.

No, keeping the container and the containee separate leads to /clearer/
(if slightly more verbose) syntax.  My proposal is to use use pointer
syntax:
  Optional!T v;
  f(v); // Do something with the container.
  f(*v); // Do something with the containee.


-- 
Rainer Deyke - rainerd at eldwood.com



More information about the Digitalmars-d mailing list