DIP60: @nogc attribute

monarch_dodra via Digitalmars-d digitalmars-d at puremagic.com
Thu Apr 17 09:42:17 PDT 2014


On Thursday, 17 April 2014 at 15:02:27 UTC, Dicebot wrote:
> === Problem #1 ===
>
> First problem is that, by an analogy with `pure`, there is no 
> such thing as "weakly @nogc@". A common pattern for performance 
> intensive code is to use output buffers of some sort:
>
> void foo(OutputRange buffer)
> {
>     buffer.put(42);
> }
>
> `foo` can't be @nogc here if OutputRange uses GC as backing 
> allocator. However I'd really like to use it to verify that no 
> hidden allocations happen other than those explicitly coming 
> from user-supplied arguments. In fact, if such "weakly @nogc" 
> thing would have been available, it could be used to clean up 
> Phobos reliably.

I don't really see how this is really any different than safe, 
nothrow or pure attributes.

Either your code is templated, and the attributes get inferred.

Or it's not templated, and you have to rely on `put`'s base-class 
signature. If it's not marked @nogc (or safe, pure, or nothrow), 
then that's that.

--------

That said, your proposal could be applied for all attributes in 
general. Not just @nogc in particular. In practice though, a 
simple unittest should cover all your needs. simply create a 
@nogc (pure, nothrow, safe, ctfe-able) unitest, and call it with 
a trivial argument. If it doesn't pass, then it probably means 
you made a gc-related (or impure, throwing, unsafe) call that's 
unrelated to the passed parameters.

In any case, that's how we've been doing it in phobos since we've 
started actually caring about attributes.


More information about the Digitalmars-d mailing list