Warn about using the GC
Alexandru Ermicioi
alexandru.ermicioi at gmail.com
Sun Jan 14 16:36:45 UTC 2024
On Sunday, 14 January 2024 at 15:40:22 UTC, Richard (Rikki)
Andrew Cattermole wrote:
> I think that you might have this slightly backwards, but please
> do clarify.
Sorry, I've reread entire thread, and it seems my suggestion is
not related to what you've proposed with @localnogc, it is
related to another problem with delegate callbacks, where you
need to mark them @nogc to be called in @nogc function.
So to make things clear about what I suggested:
```d
void perform() {
acceptDelegateWithGc(() {
Object obj = new Object();
return obj;
});
acceptNoGcDelegatesOnly(() {
Object obj = new Object();
return obj;
}); // This fails since only nogc delegates are allowed
acceptNoGcDelegatesOnly(() @trustedgc {
Object obj = new Object();
return obj;
}); // This will work since delegate is marked with
trustedgc, and therefore user knows whats he's doing when a nogc
function calls a method that uses gc.
}
void acceptDelegateWithGc(Object delegate() @trustedgc create)
@nogc {
create();
}
void acceptNoGcDelegatesOnly(Object delegate() @nogc create)
@nogc {
create();
}
```
In your hot path case, `@trustedgc` functionality might be used,
if it was possible to put it on statement level like this:
```d
void hot(float[] a, float b) @nogc {
size_t last;
float delegate() dg = () { // error: will allocate closure
context with GC
if (last > a.length)
return float.init;
return a[last++] + b;
};
@trustedgc accept(dg); // ok, accept uses GC, this call is not
checked.
}
```
This example is similar to a topic that was ongoing about
@trusted statement blocks, which I hope would be revisited in
future and implemented.
Best regards,
Alexandru.
More information about the Digitalmars-d
mailing list