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

H. S. Teoh hsteoh at quickfur.ath.cx
Sat Jan 4 15:15:54 UTC 2020


On Sat, Jan 04, 2020 at 03:50:54PM +0100, Robert M. Münch via Digitalmars-d wrote:
[...]
> class a {
> 	const myThing*;
> 
> 	init(){
> 		myThing = initMyThing();
> 	}
> 	
> 	this(){
> 		init();
> 	}
[...]

In general, it's a very bad idea to name a member function or variable
.init, because .init is supposed to be a bitwise copy of the initial
state of a type. While the language currently doesn't regard it as an
error to redefine .init to be something else, IMO it should, because it
breaks a lot of assumptions in generic code, and in general just causes
too much trouble to be worth it. I wouldn't be surprised if you came
across compiler bugs if you redefine .init this way.

If you didn't intentionally override .init, the best solution is to
rename it to something that doesn't clash with the built-in expectations
of .init, like initialize() or some such. That should fix your problem.


T

-- 
It is not the employer who pays the wages. Employers only handle the money. It is the customer who pays the wages. -- Henry Ford


More information about the Digitalmars-d mailing list