Hacker News new | ask | show | jobs
by stickfigure 4806 days ago
I can't believe there is no mention of Ceylon here. Ceylon has an elegant approach to this using union types.

    Typesafe null and flow-dependent typing

    There's no NullPointerException in Ceylon, nor anything similar. Ceylon
    requires us to be explicit when we declare a value that might be null,
    or a function that might return null. For example, if name might be null,
    we must declare it like this:

    String? name = ...

    Which is actually just an abbreviation for:

    String|Null name = ...

    An attribute of type String? might refer to an actual instance of String,
    or it might refer to the value null (the only instance of the class Null).
    So Ceylon won't let us do anything useful with a value of type String?
    without first checking that it isn't null using the special if (exists ...)
    construct.

    void hello(String? name) {
        if (exists name) {
            print("Hello, ``name``!");
        }
        else {
            print("Hello, world!");
        }
    }
From http://ceylon-lang.org/documentation/1.0/introduction/
2 comments

That is exactly equivalent to Haskell's Maybe, except I don't know if it's possible to abstract over type constructors in Ceylon to implement eg. Functor and Monad.
C# does this too, but only for primitive types (per my understanding).