This is a little Haskell program that uses the Maybe type constructor:

foo :: Int -> Maybe Int
foo x | x < 10 = Just x
foo _          = Nothing

main = do
    print $ foo 5
    print $ foo 15


Its output:

Just 5
Nothing




This is a similar D program, that uses Nullable:

import std.stdio, std.typecons;

Nullable!int foo(int x) {
    return (x < 10) ?
           typeof(return)(x) :
           typeof(return)();
}

void main() {
    writeln(foo(5));
    writeln(foo(15));
}


Its output:

5
core.exception.AssertError@C:\dmd2\src\phobos\std\typecons.d(1515): Called `get' on null Nullable!int.
...


I think it's better for write(Nullable!int()) to not raise an error, but to print something like a "<null>" etc.

A bigger problem is in the usage of Nullable. I'd like the D type system to be modified and improved to support Nullables with a nicer syntax.

Bye,
bearophile

Reply via email to