Experimentation Stage

Mutability

Lense supports the concept of instances being immutable by default. You must opt in for mutability using the mutable modifier. This applies to classes, properties, fields and variables.

Lets take an example. Consider the class Person:

public class Person {

	constructor(
		public name : String,
		public address : String,
	);

}

You can then use it like :

	let person = new Person("John", "Abbey Road");

Simple and straightforward. Now we need to change the address. In other languages you would write:

	 person.address = "New Address";

But Lense, will not allow it since the property is immutable by default. To allow mutability we must write:

public mutable class Person {

	constructor(
		public name : String,
		public mutable address : String,
	);

}

Notice the use of the mutable keyword in the property and in the class. Only mutable classes can have mutable properties.

But what about variables ? This code will fail:

	let person = new Person("John", "Abbey Road");
	person = new Person("Clarice", "Wall Street"); // error

Variables are also immutable by default. If you really need to mutate it you need to mark it mutable

	mutable let person = new Person("John", "Abbey Road");
	person = new Person("Clarice", "Wall Street"); // ok

The same principle does not apply to parameters. Parameters are always immutable since is not a good practice to mutable parameters.