Simeon
Simeon

Reputation: 7792

List of OO languages where object immutability can be compiler enforced

Can anyone give me a list of languages where class immutability can be compiler enforced and tested easily ?

I need to be able to do something like:

class immutable Person {
    private String name = "Jhon"; // lets say the String is mutable
    
    public Person(String name) {
        this.name = name; // ok
    }

    public void setName(String newName) { 
        this.name = newName; // does not compile
    }

    public void getName() { 
        return this.name; //returns reference through which name can't be mutated
    }

    private void testImmutability() {
        getName().setFirstChar('a'); // does not compile
    }
}

EDIT:

For a little more clarification, see here.

Upvotes: 6

Views: 459

Answers (4)

pblasucci
pblasucci

Reputation: 1778

F# and Scala both have the ability to created compiler-enforced immutable types (i.e. classes).

The following shows the basics in F#...

// using records is the easiest approach (but there are others)
type Person = { Name:string; Age:int; }
let p = { Person.Name="Paul";Age=31; }

// the next line throws a compiler error
p.Name <- "Paulmichael"

Here's the equivalent Scala. Note that you can still make mutable objects by using var instead of val.

class Person(val name: String, val age: Int)
val p = new Person("Paul", 31)

// the next line throws a compiler error
p.name = "Paulmichael"

Upvotes: 4

user381991
user381991

Reputation:

The D (version D2) programming language has immutability. It has OOP, but immutability is rather a concept from functional pl. There it's called purity.

Upvotes: 3

Apalala
Apalala

Reputation: 9224

Functional programming languages like OCAML, Haskell, and Erlang.

Upvotes: 5

Mike Samuel
Mike Samuel

Reputation: 120486

Joe-E

From the language spec

3.4 Immutable Types

A type T is immutable if and only if it implements the marker interface org.joe_e.Immutable according to the overlay type system. The (empty) org.joe_e.Immutable interface must be provided by the Joe-E implementation. The intuition behind an immutable object is that such an object cannot be changed (mutated) in any observable way, nor can any objects reachable by following the elds of the immutable object. The contents of an immutable objects' elds and any objects reachable from an immutable object must not change once the object is constructed. With the exception of library classes explicitly deemed to implement Immutable, an immutable class must satisfy additional linguistic restrictions enforced by the verier (x4.4) to ensure this property. Library classes that cannot be automatically verified and are deemed immutable must be carefully manually veried to expose no possibility for modication of their contents. Note that immutability does not place any restrictions on any local variables dened within the immutable class. It also says nothing about the mutability of the arguments passed to methods. It only applies to the values stored in and objects reachable from the immutable class's elds

It also introduces useful notions of powerless, and selfless types.

Upvotes: 3

Related Questions