Skip to content

Variable Declarations

Matt Bierner edited this page Apr 7, 2014 · 2 revisions

Khepri uses the same syntax as Javascript for variable declarations with a few important additions.

Mutability

Variables declared without an initializer, or using the = for the initializer, are mutable.

var a = 10, b;
b = 5; // ok
a = 2; // ok

Immutable bindings are created using an initializer and := or =::

var x := 10;
x = 10; // error

Recursive / Non Recursive

The default variable declaration declared using = will see their own binding in their initializer. This is slightly different behavior than recursive let bindings, because the binding is mutable.

// Undefined before binding
var x = x + 'z'; // "undefinedz"

// Getting most recent binding.
var f = \ -> f;
var g = f;
f = 10;
g(); // 10;

:= also creates a recursive binding that will see itself in the initializer, but the bound value is immutable.

// The inner fibs always point to the fib declared here.
var fib := \x -> ?x < 2 :x :(fib(n - 1) + fib(n - 2))'

=: also creates a non-recursive immutable binding. This is useful when the initializer must not capture itself.

// Using outer binding
var x = 10;
{
    // Use old value of x
    var x =: x + 5; // 15
}
Clone this wiki locally