How can I do that in @nogc?

Namespace via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Feb 25 12:36:32 PST 2015


On Wednesday, 25 February 2015 at 20:15:10 UTC, anonymous wrote:
> On Wednesday, 25 February 2015 at 19:32:50 UTC, Namespace wrote:
>> ----
>> void glCheck(lazy void func, string file = __FILE__, uint line 
>> = __LINE__) {
>> 	func();
>> 	glCheckError(file, line);
>> }
>> ----
>>
>> How can I specify that 'func' is @nogc? Or can define the 
>> function otherwise?
>
> First of all, if glCheck always uses/evaluates func, then there 
> is no point in making it lazy.
>
> On to the @nogc vs. lazy issue.
>
> Simpler test case:
> ---
> void glCheck(scope lazy int thing) @nogc {auto x = thing;}
> int costly() @nogc {return 42;}
> void main() @nogc
> {
>     glCheck(costly()); /* A */
>     int x; glCheck(x); /* B */
> }
> ---
>
> I guess, the compiler could see that the delegate made for the 
> lazy parameter must be @nogc. But it doesn't. So it tries to 
> call a non- at nogc delegate in a @nogc function which fails of 
> course.
>
> You can make the delegate explicit so that you can tag the 
> delegate as @nogc yourself:
> ---
> void glCheck(scope int delegate() @nogc thing) @nogc {auto x = 
> thing();}
> int costly() @nogc {return 42;}
> void main() @nogc
> {
>     glCheck(()=>costly());
>     int x; glCheck(()=>x);
> }
> ---
>
> The calls are not as nice, requiring an explicit delegate 
> ("()=>"), but it works.
>
> It may be possible to hack through this limitation - NOT 
> THOUGHT-OUT, NOT TESTED, NOT RECOMMENDED:
> ---
> void glCheck(scope lazy int thing) @nogc;
> pragma(mangle, glCheck.mangleof) void glCheckImpl(scope int 
> delegate() @nogc thing) @nogc {auto x = thing();}
> int costly() @nogc {return 42;}
> void main() @nogc
> {
>     glCheck(costly());
>     int x; glCheck(x);
> }
> ---

That last thing works. But I have no clue why. o.O
Anyway, thanks a lot!


More information about the Digitalmars-d-learn mailing list