Nullable<int> someNumber = foo.TryAndGetSomeNumber();
if(someNumber && someNumber.HasValue)
doSomething( someNumber.Value );You still have to remember to do this check. Will the compiler complain if you use someNumber.Value without checking that its valid first? If not, then its not really the same thing.
The point of Haskell's Maybe is that the compiler will make sure that you take both cases (that there is a value and that there isn't a value) into account (because to extract the value you must pattern match and pattern matches must be exhaustive, otherwise you get a compile error).
int result;
if (foo.TryAndGetSomeNumber(out result))
doSomething(result);
There might also be a foo.GetSomeNumber() that returns an int or throws on failure.[fixed formatting]
re: haskell: Unfortunately or otherwise, due to influx of devs,the nomenclature has gotten mangled re Maybe/Either and all the other things that (data) can do:
- ADT used to denote abstract data types,
- Algebraic data types used to signify sum (e.g. Maybe and Either) and product (e.g. record) types, I think many people use it for only sum types now (hard to quantify my impressions of current usage, tho)
Here's a good little tutorial: http://www.codecommit.com/blog/scala/the-option-pattern