www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How come isMutable returns true for structs that cannot be modified

reply aliak <something something.com> writes:
import std.traits: isMutable;
struct S {
     immutable int i = 3;
}

pragma(msg, isMutable!S);

void main() {
     S s;
     s = S();
}

And is there a trait that takes the transitivity of immutability 
in to account?

Cheers,
- Ali
Jul 27 2018
parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
On 7/27/18 9:10 AM, aliak wrote:
 import std.traits: isMutable;
 struct S {
      immutable int i = 3;
 }
 
 pragma(msg, isMutable!S);
 
 void main() {
      S s;
      s = S();
 }
isMutable only takes the type into account, it doesn't look to see if all the internals are mutable. It literally is this: enum bool isMutable(T) = !is(T == const) && !is(T == immutable) && !is(T == inout);
 And is there a trait that takes the transitivity of immutability in to 
 account?
I think you are looking for https://dlang.org/phobos/std_traits.html#isAssignable -Steve
Jul 27 2018
parent aliak <something something.com> writes:
On Friday, 27 July 2018 at 14:48:06 UTC, Steven Schveighoffer 
wrote:
 On 7/27/18 9:10 AM, aliak wrote:
 import std.traits: isMutable;
 struct S {
      immutable int i = 3;
 }
 
 pragma(msg, isMutable!S);
 
 void main() {
      S s;
      s = S();
 }
isMutable only takes the type into account, it doesn't look to see if all the internals are mutable. It literally is this: enum bool isMutable(T) = !is(T == const) && !is(T == immutable) && !is(T == inout);
 And is there a trait that takes the transitivity of 
 immutability in to account?
I think you are looking for https://dlang.org/phobos/std_traits.html#isAssignable -Steve
Ah! Yes I think I am. Thanks!
Jul 27 2018