www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Effect of declaring a class immutable ?

reply chmike <christophe meessen.net> writes:
I couldn't find any information about this on the dlang web site.

What is the effect adding the immutable attribute to a class like 
this

immutable class MyClass { ... }

The compiler doesn't complain.
Will it add the immutable attribute to all members ?
May 26 2016
next sibling parent ArturG <var.spool.mail700 gmail.com> writes:
On Thursday, 26 May 2016 at 14:12:23 UTC, chmike wrote:
 I couldn't find any information about this on the dlang web 
 site.

 What is the effect adding the immutable attribute to a class 
 like this

 immutable class MyClass { ... }

 The compiler doesn't complain.
 Will it add the immutable attribute to all members ?
auto mc = new MyClass; typeof(mc.someFieldOrFun).stringof.writeln; says yes and if you define opCall you need to use auto mc = new immutable MyClass;
May 26 2016
prev sibling next sibling parent Jonathan M Davis via Digitalmars-d-learn writes:
On Thursday, May 26, 2016 14:12:23 chmike via Digitalmars-d-learn wrote:
 I couldn't find any information about this on the dlang web site.

 What is the effect adding the immutable attribute to a class like
 this

 immutable class MyClass { ... }

 The compiler doesn't complain.
 Will it add the immutable attribute to all members ?
If you put any attribute on a class that's not specifically for a class (like abstract or final), then it will mark all of its members with that attribute, which is almost never what you want. It's equivalent to doing something like immutable { class MyClass { } } or class MyClass { immutable: } - Jonathan M Davis
May 26 2016
prev sibling parent Basile B. <b2.temp gmx.com> writes:
On Thursday, 26 May 2016 at 14:12:23 UTC, chmike wrote:
 I couldn't find any information about this on the dlang web 
 site.

 What is the effect adding the immutable attribute to a class 
 like this

 immutable class MyClass { ... }

 The compiler doesn't complain.
 Will it add the immutable attribute to all members ?
Since immutable is transitive everything in your class will be. So basically the only thing you can do is - create a new immutable(MyClass) - sets the instances variables in the ctor. - calls the method (which can't do anything on the variables). And that's all, e.g: ---- immutable class Foo { int i; this(int i){this.i = i;} void method(){} } void main() { immutable(Foo) foo = new immutable(Foo)(1); //foo.i = 8; // not possible since i is immutable Foo foo1 = cast(Foo) foo; // cast away immutable from the type. //foo1.method; // not pissible since method is immutable } ---- So it's more or less useless, unless you want to wrap some variables in a class to simplify completion in the IDE or whatever other reasons. _________________ By the way the doc for "immutable class Stuff" is here: https://dlang.org/spec/const3.html#immutable_type, it's a type constructor. "immutable" is fully part of the type.
May 26 2016