Quote:Original post by Antheus
Quote:Original post by davepermen
optional makes this explicit.
Don't really see how. Why was optional<> not set? Was it an error? Was it normal return?
As demonstrated above - optional<> too can be set, yet contain invalid value.
It carries precisely the same semantic information as regular pointer.
optional<> | T*-----------------+---------------------------not_set | NULLset + value | some pointerset + corrupt | invalid pointerset + NULL | ??? |if (p) { | if (p) {} // what if NULL)}
the trick is to NOT EVER USE optional in case of an erroneous state. you just use it where it's expected to consider both cases as valid. and ONLY then.
null pointer should never happen, it should always be error.
that is the distinction to make, and it's an important one. important enough for me to varrant different code constructs to represent them. just as i use exception handling for the error handling, and state-returns for actual application logic.
but i gave enough examples (think of "GetNearestHitPoint (or in my case RayTrace)". that is a typical function made for optional. optional<Hit>). and enough where you don't want optional.
the distinction should be obvious to anyone, but apparently isn't. well, to those that is, they see the point of optional. the others still hack around in their lowlevel "you just have to know it". i prefer to use the language to
the explain it. esp when writing a library / class that others will use.
oh, and, just in case, optional is a standard language feature in c# and works wonderfully. and pointers aren't (for safe code), and that works wonderfully, too. (but optional on gc types doesn't, sadly, so the consistency is a bit lost, there :( )