On assigning const to immutable
FeepingCreature
feepingcreature at gmail.com
Thu Jul 13 11:59:56 UTC 2023
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.
More information about the Digitalmars-d-learn
mailing list