/
Declaration Semantics

Declaration Semantics

KRL declarations are not variable assignments. And KRL uses static binding semantics. As a result you may be surprised if you redeclare a specific variable. 

For example, consider the following declarations:

pre {
  x = 3;
  y = function(a){x + a};
  z = {"foo": 3}
}

Internally, this declaration block produces the following environment:

When we make a function call like y(6), the result will be 9 because x is defined globally, from y's perspective. 

Now supposed we have a block that redeclares x to be 5.

pre {
  x = 3;
  y = function(a){x + a};
  z = {"foo": 3}
  x = 5;
}

The resulting environment looks like this:

Note that the first x is still there. Code referencing x in this environment will start looking for x from the bottom and see 5 since that declaration shadows the first. 

But, when we call y(6), the result will still be 9 not 11 because the function sees the environment from it's perspective and so x is still 3. 

This is not how a language with assignment would behave. Instead of rebinding x to 5, such a language would update the original x through assignment and when y(6) executes, the result would be 11. 

Related content

Using Optional and Named Parameters
Using Optional and Named Parameters
More like this
Arithmetic Expressions
Arithmetic Expressions
Read with this
Declarations
Declarations
More like this
String Operators
String Operators
Read with this
KRL misconceptions and common errors
KRL misconceptions and common errors
More like this
Universal Operators
Universal Operators
Read with this

Copyright Picolabs | Licensed under Creative Commons.