www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - union mutability

reply Jack Applegame <japplegame gmail.com> writes:
Code:

union A {
     immutable int f;
}

union B {
     immutable int f;
     int e;
}

void main() {
     A a = A(1);
     //a = A(2); // a.f is immutable, fails to compile as expected
	
     B b = B(1);
     b = B(2); // compiles!!!
}

It turns out that if the union contains at least one mutable 
member, then the entire union is considered to be mutable.
It's logical, but does it meet the specs? I couldn't find 
description of this behavior.
Aug 25 2016
next sibling parent Jonathan M Davis via Digitalmars-d-learn writes:
On Thursday, August 25, 2016 15:22:23 Jack Applegame via Digitalmars-d-learn 
wrote:
 Code:

 union A {
      immutable int f;
 }

 union B {
      immutable int f;
      int e;
 }

 void main() {
      A a = A(1);
      //a = A(2); // a.f is immutable, fails to compile as expected

      B b = B(1);
      b = B(2); // compiles!!!
 }

 It turns out that if the union contains at least one mutable
 member, then the entire union is considered to be mutable.
 It's logical, but does it meet the specs? I couldn't find
 description of this behavior.
Well, Rebindable depends on it (though Rebindable is in a legal grey area at best and technically in violation of the rules at worst). It's basically the same as when you cast away const or immutable - it's fine as long as you don't mutate the variable and thus break the guarantees that compiler has for const or immutable objects. But I don't know how you can really do anything with it without violating immutable. Certainly, in any case other than what Rebindable is up to, I wouldn't do it, and even then, I suspect that Rebindable is in a situation where it's technically violating the compiler's guarantees but does so in a way that will never actually result in problems in practice. But yes, this behavior is known, albeit questionable. - Jonathan M Davis
Aug 25 2016
prev sibling parent reply Meta <jared771 gmail.com> writes:
On Thursday, 25 August 2016 at 15:22:23 UTC, Jack Applegame wrote:
 Code:

 union A {
     immutable int f;
 }

 union B {
     immutable int f;
     int e;
 }

 void main() {
     A a = A(1);
     //a = A(2); // a.f is immutable, fails to compile as 
 expected
 	
     B b = B(1);
     b = B(2); // compiles!!!
 }

 It turns out that if the union contains at least one mutable 
 member, then the entire union is considered to be mutable.
 It's logical, but does it meet the specs? I couldn't find 
 description of this behavior.
This should be fixed pretty soon: https://github.com/dlang/dmd/pull/5940
Aug 25 2016
parent reply Jack Applegame <japplegame gmail.com> writes:
On Thursday, 25 August 2016 at 17:01:40 UTC, Meta wrote:
 This should be fixed pretty soon: 
 https://github.com/dlang/dmd/pull/5940
Bye-bye immutable classes. :'(
Aug 25 2016
parent reply Jonathan M Davis via Digitalmars-d-learn writes:
On Thursday, August 25, 2016 18:27:25 Jack Applegame via Digitalmars-d-learn 
wrote:
 On Thursday, 25 August 2016 at 17:01:40 UTC, Meta wrote:
 This should be fixed pretty soon:
 https://github.com/dlang/dmd/pull/5940
Bye-bye immutable classes. :'(
Why? I don't know exactly what that PR is supposed to do, but std.datetime uses immutable time zone objects, and if that PR made it so that you couldn't have an immutable instance of a class, then it would have failed the auto-tester. And it clearly doesn't stop having unions with immutable members, or it would have failed to compile because of Rebindable (though it may be that Rebindable can no longer be used in safe code). - Jonathan M Davis
Aug 25 2016
parent reply Jack Applegame <japplegame gmail.com> writes:
On Thursday, 25 August 2016 at 19:19:49 UTC, Jonathan M Davis 
wrote:
 Why? I don't know exactly what that PR is supposed to do, but 
 std.datetime uses immutable time zone objects, and if that PR 
 made it so that you couldn't have an immutable instance of a 
 class, then it would have failed the auto-tester. And it 
 clearly doesn't stop having unions with immutable members, or 
 it would have failed to compile because of Rebindable (though 
 it may be that Rebindable can no longer be used in  safe code).

 - Jonathan M Davis
Because lack of mutable references to immutable classes in safe code is big PITA for me. It is easier to not use immutable classes at all.
Aug 25 2016
parent reply Jack Applegame <japplegame gmail.com> writes:
Also I hate Rebindable.
Aug 25 2016
parent Jonathan M Davis via Digitalmars-d-learn writes:
On Thursday, August 25, 2016 19:43:49 Jack Applegame via Digitalmars-d-learn 
wrote:
 Also I hate Rebindable.
Yeah, well, without a language change, it's the best that we have for dealing with the problem that it's designed to solve. D just isn't designed with the idea that there's any distinction between the type of a class reference and the type of the class it refers to. - Jonathan M Davis
Aug 25 2016