Why allow initializers of non-static members that allocate?

Bastiaan Veelo Bastiaan at Veelo.net
Sat Jun 11 10:37:30 UTC 2022


On Saturday, 11 June 2022 at 01:52:58 UTC, Mike Parker wrote:
> People getting bit by `new` in field initialization often 
> enough that I think a warning would be helpful.

The problem is so much bigger because it is not just a case of 
being aware not to use `new` in member initialisers. As you see 
in my second example in 
https://forum.dlang.org/post/ogvubzgprghefclgluce@forum.dlang.org 
there is no `new` anywhere. In fact you could say an effort has 
been made to do the right thing in `struct A`  where the 
allocation has been moved to the constructor (a strategy that is 
not always available because structs don’t have default 
constructors) yet we fell into the same trap.

My point is that this problem can be buried deep down under 
multiple layers and you can’t really ever be sure that there 
isn’t a problem in your massive code base.

> But any such warnings need to be enabled by default to be 
> useful, and must have an off switch for people who don't need 
> them. So the question in each case would be, where's the line 
> between helpful and annoying?

So that’s why I used “why” in the title of this thread, which I 
haven’t seen an answer to yet. What is the practical case where 
that warning would be annoying? When would you actually want this 
behaviour?

— Bastiaan.




More information about the Digitalmars-d-learn mailing list