Module-level attributes and unit tests

monarch_dodra via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Aug 23 09:22:03 PDT 2014


On Saturday, 23 August 2014 at 15:26:02 UTC, Leandro Motta Barros 
via Digitalmars-d-learn wrote:
> Hello,
>
> I have a module which is completelly @nogc, and as such I'd 
> like to just say
>
>    @nogc:
>
> at the top of the file and be happy.
>
> However, my unit tests for this same module do some GC 
> allocation, so the
> module fails to compile.
>
> Is there a way to disable @nogc for the unit tests only? Would 
> this be a
> bad idea in some way I cannot see?

Nope. At best, you might be able to *declare* a function that is 
GC, but even then, you wouldn't be able to call it.

> I suppose the same question is valid for other attributes, like 
> 'nothrow'
> and '@safe'.

Actually, you can "undo" @safe with an explicit @system. nothrow 
(and pure) are in the same boat as @nogc

> Thank you,
>
> LMB

Solutions I can see would be to:
a) Put your unittests in a separate module.
b) Place your unittests before your "@nogc:"
c) Instead of using "@nogc:", use "@nogc {}": This will make 
@nogc run until the end of the block.


More information about the Digitalmars-d-learn mailing list