Scope in Reason

Nik Graf
InstructorNik Graf

Share this video with your friends

Send Tweet
Published 4 years ago
Updated 3 years ago

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

Instructor: [00:00] 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.

[00:09] 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.

[00:29] 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.

[00:53] 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.

[01:25] 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.