On assigning const to immutable

Ki Rill rill.ki at yahoo.com
Thu Jul 13 11:56:39 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?

Oh, is it because the first one is passed by value, but the 
second one is a reference?


More information about the Digitalmars-d-learn mailing list