How can I do that in @nogc?

anonymous via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Feb 25 12:15:09 PST 2015


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);
}
---


More information about the Digitalmars-d-learn mailing list