Templates: generic "return null;"

Stanislav Blinov stanislav.blinov at gmail.com
Mon Feb 3 03:31:21 PST 2014


On Monday, 3 February 2014 at 10:55:23 UTC, Chris wrote:

> I'm reluctant to (over)use throw, because I think that throw 
> should be the last resort when you cannot easily predict all 
> the things that can go wrong. Simple requests should give 
> simple answers. If the key doesn't exist it returns nothing.

D has no concept of "nothing" in the language. What if your 
dictionary contains objects, and for some key K it *does* contain 
a null reference? How would you distinguish absence of value from 
some "invalid" value? Nullable can help with that, of course. But 
then again, what if your dictionary does contain other Nullables? 
:)
Generally, inability to return value is an exceptional situation.

> The problem above only exists because of generic types, else it 
> would be either 'null' or for numbers -1 or something.

-1 is a number, just like 0 or 10 or 13142313. Same as above, at 
the call site you won't be able to tell if -1 indicates that 
there's no such key in the attributes dictionary or just that 
attribute happens to have a value of -1.



More information about the Digitalmars-d-learn mailing list