Comment #0 by bearophile_hugs — 2014-04-27T13:44:34Z
(Issue understood with a little help from Dicebot.)
This compiles:
void foo(int delegate() @nogc x) @nogc {
auto r = x();
}
void main() {
foo(() => 1);
}
This too compiles, performing inference:
void foo(Deleg)(Deleg x) @nogc {
auto r = x();
}
void main() {
foo(() => 1);
}
But this code:
void foo(lazy int x) @nogc {
auto r = x(); // Error
}
void main() {
foo(1);
}
Gives with DMD 2.066alpha:
test.d(2,15): Error: @nogc function 'test.foo' cannot call non-@nogc delegate 'x'
I think the third program should compile.
Comment #1 by dfj1esp02 — 2014-04-27T14:27:52Z
The third example can't infer attributes because it doesn't create the delegate, and can't accept arbitrary type info because it's not templated.
Comment #2 by bearophile_hugs — 2014-04-27T14:57:34Z
(In reply to Sobirari Muhomori from comment #1)
> The third example can't infer attributes because it doesn't create the
> delegate, and can't accept arbitrary type info because it's not templated.
Thank you, I understand now :-)
Is it a good idea to allow annotations for lazy arguments?
void foo(T)(lazy @nogc x) @nogc {
auto r = x();
}
void main() {
foo(1);
}
I guess lazy arguments are not used commonly enough to deserve such improvements.
Comment #3 by bearophile_hugs — 2014-04-27T14:59:28Z
(In reply to bearophile_hugs from comment #2)
> void foo(T)(lazy @nogc x) @nogc {
> auto r = x();
> }
> void main() {
> foo(1);
> }
Sorry, I meant to write:
void foo(lazy @nogc x) @nogc {
Comment #4 by dfj1esp02 — 2014-04-27T14:59:50Z
Yes, annotation could do the trick.
Comment #5 by k.hara.pg — 2014-04-28T05:25:16Z
Lazy argument is implicitly converted to "scope local" delegate. And the original argument @safe-ty and purity are implicitly treated as they belong to the caller side.
int foo(lazy int x) pure @safe { return x(); }
int get() { return 1; } // impure un@safe
void main()
{
foo(get()); // OK!
}
So I think that nothrow-ness and @nogc-ability should be treated as same.
*** This issue has been marked as a duplicate of issue 12647 ***