www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - On assigning const to immutable

reply Ki Rill <rill.ki yahoo.com> writes:
Why does the first example `class A` work, but the second one 
with `class B` does not?
```D
class A {
     immutable int a;
     this(in int a) {
     	this.a = a;
     }
}

class B {
	immutable int[] b;
     this(in int[] b) {
     	this.b = b;
     }
}

void main()
{
     auto a = new A(1);
     auto b = new B([1, 2]);
}
```

It implicitly converts `const` to `immutable`, but fails to do 
the same with an array. Is it intentional? Why?
Jul 13 2023
next sibling parent Ki Rill <rill.ki yahoo.com> writes:
On Thursday, 13 July 2023 at 11:55:17 UTC, Ki Rill wrote:
 Why does the first example `class A` work, but the second one 
 with `class B` does not?
 ```D
 class A {
     immutable int a;
     this(in int a) {
     	this.a = a;
     }
 }

 class B {
     immutable int[] b;
     this(in int[] b) {
     	this.b = b;
     }
 }

 void main()
 {
     auto a = new A(1);
     auto b = new B([1, 2]);
 }
 ```

 It implicitly converts `const` to `immutable`, but fails to do 
 the same with an array. Is it intentional? Why?
Oh, is it because the first one is passed by value, but the second one is a reference?
Jul 13 2023
prev sibling parent FeepingCreature <feepingcreature gmail.com> writes:
On Thursday, 13 July 2023 at 11:55:17 UTC, Ki Rill wrote:
 Why does the first example `class A` work, but the second one 
 with `class B` does not?
 ```D
 class A {
     immutable int a;
     this(in int a) {
     	this.a = a;
     }
 }

 class B {
 	immutable int[] b;
     this(in int[] b) {
     	this.b = b;
     }
 }

 void main()
 {
     auto a = new A(1);
     auto b = new B([1, 2]);
 }
 ```

 It implicitly converts `const` to `immutable`, but fails to do 
 the same with an array. Is it intentional? Why?
Yep: `immutable` is data that *cannot ever* change. If you could convert `int[]` to `immutable int[]`, you could do this: ``` int[] a = [2]; immutable(int)[] b = a; auto first = b[0]; a[0] = 3; auto second = b[0]; // Wait, this fails? I thought `b` was immutable. assert(first == second); ``` Use `array.idup` (immutable dup) to turn `int[]` into `immutable int[]`. And you can convert `int` to `immutable int` because it's a "value copy": you cannot mutate the immutable variable through an assignment to the original. Generally speaking, only the constness of referenced data matters for assignment. That's why you can assign `string` to `immutable string`, because `string` is `immutable(char)[]`: the referenced data is immutable in both cases.
Jul 13 2023