Re: Construct immutable member in derived class

2018-04-05 Thread Alex via Digitalmars-d-learn
On Thursday, 5 April 2018 at 19:31:39 UTC, Jonathan M Davis wrote: And you can't abstract whether a member variable is marked with immutable or not. That's part of the variable. Declaring an immutable instance of an object would then treat the member variable in immutable for that instance, so

Re: Construct immutable member in derived class

2018-04-05 Thread Jonathan M Davis via Digitalmars-d-learn
On Thursday, April 05, 2018 13:36:07 Alex via Digitalmars-d-learn wrote: > On Wednesday, 4 April 2018 at 21:49:08 UTC, Timoses wrote: > > "[...] the construction of the base class can be independent > > from the derived one." > > > > Hm, the points 7 and 8 don't clearly state what you wrote. > >

Re: Construct immutable member in derived class

2018-04-05 Thread Timoses via Digitalmars-d-learn
On Wednesday, 4 April 2018 at 22:47:07 UTC, Jonathan M Davis wrote: Because doing that basically makes it impossible to guarantee that the type system isn't violated. Once an immutable variable has been initialized, its value must _never_ change. It must be initalized exactly once, and the

Re: Construct immutable member in derived class

2018-04-05 Thread Alex via Digitalmars-d-learn
On Wednesday, 4 April 2018 at 21:49:08 UTC, Timoses wrote: "[...] the construction of the base class can be independent from the derived one." Hm, the points 7 and 8 don't clearly state what you wrote. Yes :) But it somehow does make sense.. Still I wonder why that is so. Let's say you

Re: Construct immutable member in derived class

2018-04-04 Thread Jonathan M Davis via Digitalmars-d-learn
On Wednesday, April 04, 2018 21:46:13 Timoses via Digitalmars-d-learn wrote: > On Wednesday, 4 April 2018 at 18:11:12 UTC, Jonathan M Davis > > That code doesn't compile - at least not with dmd master. It > > gives these two errors: > > > > q.d(5): Error: constructor `q.A.this` missing initializer

Re: Construct immutable member in derived class

2018-04-04 Thread Timoses via Digitalmars-d-learn
On Wednesday, 4 April 2018 at 16:16:24 UTC, Alex wrote: Here is something: https://dlang.org/spec/class.html#constructors By the rules 7 and 8 it is suggested, what Simen already said, the construction of the base class can be independent from the derived one. And as such, the immutability

Re: Construct immutable member in derived class

2018-04-04 Thread Timoses via Digitalmars-d-learn
On Wednesday, 4 April 2018 at 18:11:12 UTC, Jonathan M Davis wrote: On Wednesday, April 04, 2018 16:05:52 Timoses via ``` class A { immutable int i; this(){} } class B : A { this() { this.i = 3; super(); // <- specifically calling

Re: Construct immutable member in derived class

2018-04-04 Thread Jonathan M Davis via Digitalmars-d-learn
On Wednesday, April 04, 2018 16:05:52 Timoses via Digitalmars-d-learn wrote: > On Wednesday, 4 April 2018 at 10:41:52 UTC, Simen Kjærås wrote: > > Because by the time B's constructor is called, A might already > > have initialized it, and rely on it never changing. > > What about: > > ``` > class

Re: Construct immutable member in derived class

2018-04-04 Thread Alex via Digitalmars-d-learn
On Wednesday, 4 April 2018 at 16:05:52 UTC, Timoses wrote: This becomes a bit hideous, unfortunately, when there are many initializations involved. Found this, but it doesn't mention anything about derived classes.. https://dlang.org/spec/class.html#field-init Here is something:

Re: Construct immutable member in derived class

2018-04-04 Thread Timoses via Digitalmars-d-learn
On Wednesday, 4 April 2018 at 10:41:52 UTC, Simen Kjærås wrote: Because by the time B's constructor is called, A might already have initialized it, and rely on it never changing. What about: ``` class A { immutable int i; this(){} } class B : A { this() { this.i = 3;

Re: Construct immutable member in derived class

2018-04-04 Thread Simen Kjærås via Digitalmars-d-learn
On Wednesday, 4 April 2018 at 10:11:37 UTC, Timoses wrote: Example: ``` class A { immutable int i; this(){} } class B : A { this() { this.i = 3; } } void main() { auto b = new B; } ``` throws: Error: constructor `onlineapp.A.this` missing initializer for