www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - how to declare an immutable class?

reply Charles Hixson via Digitalmars-d-learn writes:
I want to declare a class all instances of which will be immutable, and 
all references to which will be inherently immutable (so that I don't 
need to slip a huge number of "immutable" statements in my code).

This is surely possible, because string acts just that way, but I can't 
figure out how to do this.

immutable class    Msg  {    this(...) immutable{...} ... }

doesn't work that way, as when I do

Msg m = new Msg (...);

I get:

Error: incompatible types for ((this.m) - (m)): 'immutable(Msg)' and 
'cellram.Msg'

and

Error: immutable method cellram.Msg.this is not callable using a mutable 
object


Does anyone know the correct approach?
Aug 11 2016
parent sldkf <sldkf sldkf.fr> writes:
On Thursday, 11 August 2016 at 17:56:59 UTC, Charles Hixson wrote:
 Does anyone know the correct approach?
I do: °°°°°°°°°°°°°°°°°°°°°°°°°°°°°° immutable class Foo { this() {} } void main() { auto foo = new immutable(Foo); } °°°°°°°°°°°°°°°°°°°°°°°°°°°°°° But take care because you can't do much with an immutable class.
Aug 11 2016