Experimentation Stage

Variables - Imutable and Mutable

Lense is designed with immutability in mind so variables are immutable by default. You must opt in for mutability.

Creating an imutable variables, also known as constant in Lense is very similar to other languages.

let  name : String = "Alice";

Variables always contain references to objects. The variable called name contains a reference to a String object with a value of Alice. The reference contained in a variable can be changed further down in the code to another reference. But for that we have to opt in for mutability, like :

mutable let name : String = "Alice";
name = "Beth";

Lense as type inference, so variable declaration can be simplified to :

mutable let  name = "Alice";

This is the main reason why type annotations are included after the name of the variable, so they can be removed when they are not necessary. Notice that they are still relevant in the present of polymorfism like in:

let  name : Sequence<Character> = "Alice";

Definition and Initialization

In Lense you can define the value or the variable and initialize the value later, like this:

let name : String;

name = "Alice";

But, because in Lense there are no nulls, the compiler will track that the variable has been initialized to a proper value before it is used. So the following will produce an error:

let name : String;

let nameSize = name.size; // Compilation error. Variable 'name' has not be initialized yet

name = "Alice";