"constructor missing initializer for const field" when field is set inside an init function

Paul Backus snarwin at gmail.com
Sun Jan 5 15:22:28 UTC 2020


On Saturday, 4 January 2020 at 14:50:54 UTC, Robert M. Münch 
wrote:
> This following code gives an error: "constructor a.this missing 
> initializer for const field myThing"
>
> class a {
> 	const myThing*;
>
> 	init(){
> 		myThing = initMyThing();
> 	}
> 	
> 	this(){
> 		init();
> 	}
> }
>
> I have to explicitly set the const field inside this(). As long 
> as I only have one constructor that's OK. I prefer to have an 
> init() function to collect all things to setup an object.
>
> Shouldn't the compiler find out that myThing is set during 
> construction in this case?

You could try something like this:

enum string initThing = q{
     myThing = initMyThing();
};

this() {
     mixin(initThing);
}


More information about the Digitalmars-d mailing list