Re: immutable array in constructor

2016-03-20 Thread tsbockman via Digitalmars-d-learn
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

Re: immutable array in constructor

2016-03-19 Thread Jeff Thompson via Digitalmars-d-learn
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: 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

Re: immutable array in constructor

2016-03-19 Thread Jeff Thompson via Digitalmars-d-learn
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

Re: immutable array in constructor

2016-03-19 Thread Anonymouse via Digitalmars-d-learn
, 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

Re: immutable array in constructor

2016-03-19 Thread Ali Çehreli via Digitalmars-d-learn
On 03/17/2016 09:32 AM, Jeff Thompson wrote: 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: This is a simplified example from a larger class I have where I need an immutable constructor. This is because I need

Re: immutable array in constructor

2016-03-19 Thread tsbockman via Digitalmars-d-learn
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

immutable array in constructor

2016-03-18 Thread Jeff Thompson via Digitalmars-d-learn
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

Re: immutable array in constructor

2016-03-18 Thread Rene Zwanenburg via Digitalmars-d-learn
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