Warn about using the GC

Walter Bright newshound2 at digitalmars.com
Sun Jan 14 19:23:24 UTC 2024


On 1/14/2024 4:47 AM, Richard (Rikki) Andrew Cattermole wrote:
> ```d
> void func(void delegate() @nogc del) @nogc;
> ```
> 
> Basically if you pass in a delegate that does not have ``@nogc``, the ``@nogc`` 
> on the function gets removed.
> 
> The function body will of course be typed checked as if it did have ``@nogc``.
> 
> I want this to be the default, although Timon wants it to be much more 
> configurable so you can pick and choose per parameter (again not mutually 
> exclusive things!).

The trouble with having a nogc function calling a gc delegate is it makes the 
nogc function a gc function. (If the nogc function does not actually call the gc 
delegate, but just stores it somewhere, that works fine.) If the nogc was 
silently removed, wouldn't that be very surprising behavior?

There is another way, though. In @system code, the gc delegate can be forcibly 
cast to be @nogc (as far as the type system is concerned - it doesn't change the 
behavior of the delegate).

Such forcible casting can be done with any of the attributes. However, as this 
subverts the type system, the correctness of it would be entirely up to the user.

D has overrides like this because it's a systems programming language.



More information about the Digitalmars-d mailing list