Become a member
to unlock all features

Level Up!

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


    Scope in Reason


    Reason has lexical scope. In this lesson we explore what this means for the visibility of bindings using local block scopes.



    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: Reason has lexical scoping. Don't worry in case you aren't familiar with this term, as in this lesson, I will guide you through the relevant implications.

    First, we create the local scope using curly braces. It can contain one or multiple imperative statements, while the last one will automatically be returned. In this case, the value of the scope is the same as if we would type 42.

    Inside a scope, we can access bindings outside of its current scope, but let bindings defined inside a scope aren't accessible from the outside. In fact, we can shadow a let binding inside a scope, and it won't affect the let bindings outside of this local scope, even with different types.

    Here, we bind the string Hello to the name foo. Then we create the scope where we bind the integer two to the name foo again. If you refer to foo outside of the block, it's still Hello. Since every block returns the last statement as an expression, we can also bind the result to a name.

    In the coming lessons, we will see blocks being used many times to create a local scope for constructs like switch expressions or function definitions. Keep in mind, every time curly braces are used in Reason, it's the same scoping behavior.