Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


    Using Reason's Type option


    ReasonML doesn't null. Still sometimes we want to refer to something and don't know ahead if a corresponding value will be available. Reason therefor offers the Variant option shipping with the standard library.



    Become a Member to view code

    You must be a Pro Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    orLog In




    Instructor: Before we explore the type option, you need to understand why it's needed. For example, if you try to bind null to the integer H, it doesn't work. Null simply does not exist.

    This is great, since null is the cause for many, many bugs. In fact, its inventor, Sir Tony Hoare, stated, "I call it my billion-dollar mistake, was the invention of the null reference in 1965."

    Nevertheless, null serves a purpose. Sometimes you want to refer to something, and don't know ahead if the corresponding value will be available. Reason therefore offers the variant option, shipping with the standard library.

    An option has two constructors, for one's none, indicating that no value is available, and then some, indicating that there is a value. It is passed in as an argument of any type. We combine any of those to name. As with any other variant, we can use it in combination with the switch expression.

    This is really great. With option, we have a tool at our disposal that allows us to simulate a novel value while still being type safe, meaning a pure Reason program doesn't have null errors. What a blaze.