digitalmars.D.learn - immutable array in constructor
- Jeff Thompson (16/16) Mar 17 2016 In the following code, I explicitly declare array as immutable.
- Anonymouse (3/20) Mar 17 2016 The error message isn't very good, but remove immutable from the
- Jeff Thompson (5/27) Mar 17 2016 This is a simplified example from a larger class I have where I
- tsbockman (5/9) Mar 17 2016 The "mutable object" the compiler is complaining about is the
- Rene Zwanenburg (4/8) Mar 17 2016 In that case, new immutable C() should work I believe. Also, if
- tsbockman (4/6) Mar 17 2016 Ah! That's a good tip. Now I understand why I never have to say
- Jeff Thompson (2/10) Mar 17 2016 new immutable C() worked! Thanks for the insight.
- =?UTF-8?Q?Ali_=c3=87ehreli?= (8/19) Mar 17 2016 In case it's useful to others, I have qualified constructors covered at
In the following code, I explicitly declare array as immutable. But it compiles with the error shown below in the comment. The array object is declared immutable, so how can the compiler say it is a mutable object? In summary, how to pass an immutable array to an immutable constructor? class C { int i; this(immutable int[] array) immutable { i = array[0]; } } void func() { immutable int[] array = [1]; auto c = new C(array); // Error: immutable method C.this is not callable using a mutable object }
Mar 17 2016
On Thursday, 17 March 2016 at 09:57:37 UTC, Jeff Thompson wrote:In the following code, I explicitly declare array as immutable. But it compiles with the error shown below in the comment. The array object is declared immutable, so how can the compiler say it is a mutable object? In summary, how to pass an immutable array to an immutable constructor? class C { int i; this(immutable int[] array) immutable { i = array[0]; } } void func() { immutable int[] array = [1]; auto c = new C(array); // Error: immutable method C.this is not callable using a mutable object }The error message isn't very good, but remove immutable from the constructor and it works.this(immutable int[] array) {
Mar 17 2016
On Thursday, 17 March 2016 at 10:04:53 UTC, Anonymouse wrote:On Thursday, 17 March 2016 at 09:57:37 UTC, Jeff Thompson wrote:This is a simplified example from a larger class I have where I need an immutable constructor. This is because I need to construct an object an pass it to other functions which take an immutable object. So, how to keep an immutable constructor?In the following code, I explicitly declare array as immutable. But it compiles with the error shown below in the comment. The array object is declared immutable, so how can the compiler say it is a mutable object? In summary, how to pass an immutable array to an immutable constructor? class C { int i; this(immutable int[] array) immutable { i = array[0]; } } void func() { immutable int[] array = [1]; auto c = new C(array); // Error: immutable method C.this is not callable using a mutable object }The error message isn't very good, but remove immutable from the constructor and it works.this(immutable int[] array) {
Mar 17 2016
On Thursday, 17 March 2016 at 10:11:43 UTC, Jeff Thompson wrote:This is a simplified example from a larger class I have where I need an immutable constructor. This is because I need to construct an object an pass it to other functions which take an immutable object. So, how to keep an immutable constructor?The "mutable object" the compiler is complaining about is the instance of C being constructed, not the array. Change `new C(array)` to `new immutable(C)(array)` and it should work.
Mar 17 2016
On Thursday, 17 March 2016 at 10:11:43 UTC, Jeff Thompson wrote:This is a simplified example from a larger class I have where I need an immutable constructor. This is because I need to construct an object an pass it to other functions which take an immutable object. So, how to keep an immutable constructor?In that case, new immutable C() should work I believe. Also, if you mark the constructor as pure, new C() should be implicitly convertible to an immutable C.
Mar 17 2016
On Thursday, 17 March 2016 at 11:27:01 UTC, Rene Zwanenburg wrote:Also, if you mark the constructor as pure, new C() should be implicitly convertible to an immutable C.Ah! That's a good tip. Now I understand why I never have to say `new immutable(C)()` in my own code. (I am in the habit of marking everything I can as `pure`.)
Mar 17 2016
On Thursday, 17 March 2016 at 11:27:01 UTC, Rene Zwanenburg wrote:On Thursday, 17 March 2016 at 10:11:43 UTC, Jeff Thompson wrote:new immutable C() worked! Thanks for the insight.This is a simplified example from a larger class I have where I need an immutable constructor. This is because I need to construct an object an pass it to other functions which take an immutable object. So, how to keep an immutable constructor?In that case, new immutable C() should work I believe. Also, if you mark the constructor as pure, new C() should be implicitly convertible to an immutable C.
Mar 17 2016
On 03/17/2016 09:32 AM, Jeff Thompson wrote:On Thursday, 17 March 2016 at 11:27:01 UTC, Rene Zwanenburg wrote:In case it's useful to others, I have qualified constructors covered at the following link (without the compilation error that you've faced but still pointing out the fact that the mutable constructor may be called unintentionally): http://ddili.org/ders/d.en/special_functions.html#ix_special_functions.qualifier,%20constructor AliOn Thursday, 17 March 2016 at 10:11:43 UTC, Jeff Thompson wrote:new immutable C() worked! Thanks for the insight.This is a simplified example from a larger class I have where I need an immutable constructor. This is because I need to construct an object an pass it to other functions which take an immutable object. So, how to keep an immutable constructor?In that case, new immutable C() should work I believe. Also, if you mark the constructor as pure, new C() should be implicitly convertible to an immutable C.
Mar 17 2016