All Downloads are FREE. Search and download functionalities are using the official Maven repository.

reference.language.mutability.html Maven / Gradle / Ivy




    
    Mutability
    
    

    
    

    
    

    
    
    
    


Mutability

Programs are used to alter data. They do this either by introducing new data, transforming existing data or deleting it.
Naturally, many programming languages let developers alter data: C, C++, Java, C#, Python, JavaScript...

As an example, a JavaScript object can be modified as follows:

var person = { name : "John", gender : "Male", profession: "Engineer" }
// now modify the person
person.profession = "Manager";

The above has the benefit of simplicity. However, as programs grow and become more complex, notably when multiple executions threads can run in parallel, it can become very difficult to control what happens when data used by the program changes.
This has led the industry to provide a different breed of programming languages, known as functional languages.
Examples of such languages are Haskell, Erlang, F#, Clojure, and to a certain extent Scala.
Their common characteristic is that instead of modifying existing data, they rather produce new data from the existing data and the required alterations.

As an example, the above fragment could be written functionally as follows:

var person = { name : "John", gender : "Male", profession: "Engineer" };
// create a function to produce a modified person
function modifiedPersonWithAttribute(person, atttribute, newValue) {
    person = Object.create(person);
    person[atttribute] = newValue;
    return person;
}
// call that function
person = modifiedPersonWithAttribute(person, "profession", "Manager");

Notice that the original object is not modified, instead a new object is created, and assigned to the person variable. By doing so, if any other variable refers to the original object, its behavior will remain predictable.
See Functional_programming for more background on functional programming.

At Prompto, we believe in functional programming, however we also notice a few things:

  • It is a bit awkward for a language to not alter data when the purpose of every program is actually to alter data (if you accept to consider that creating new data is altering the set of existing data). This contradiction sometimes generates a lot of complexity.
  • While it is generally wrong to share mutable data across parts of a program, there are places where it is 100% safe, much simpler and much faster to mutate data, such as input/output and initialization.
From there, we decided it would be better, rather than force developers into a stiff model, to instead let them decide when to use mutable data.

Mutable variables vs mutable values

In Prompto, all variables are mutable. It is perfectly legal to write the following 2 assignments:

a = 1.347
a = 2.013

In this example, the value 1.347 remains unchanged, but a now refers to another value. Prompto values on the other hand are by default immutable. For example, you cannot change the value of a default non mutable list, instead you must produce a new list composed from the old list and additional data, as follows:

list = [1, 2, 3]
list = list + [4, 5]

Now let's imagine you need to change the second item of the above list from 2 to 5. Using an immutable list, you would have to write the following:

list = list[1:1] + [5] + list[3:3]

This is tedious to write, hard to read, and slow to execute. If list only exists within a small and well controlled scope, it is much simpler and faster to use a mutable list instead, as follows:

list = mutable [1, 2, 3]
list[2] = 5

Please note that the above code would be illegal if the mutable keyword was omitted.

Also note that only collection and category values can be marked mutable.

Mutability guardians

Controlling mutability can rapidly become complex when values are passed as parameters to methods, or when returning them from method bodies.
Prompto lets developers declare mutable parameters, using the following syntax:

define doStuff as method receiving mutable Text[] list and Integer[] someData

In this example, the list parameter is mutable, while the someData parameter is not.
Prompto ensures that only mutable values are passed as mutable parameters, hence preventing methods from modifying data without notice.





© 2015 - 2025 Weber Informatics LLC | Privacy Policy