digitalmars.D.learn - must scope for delegates restrict compilation?
- Oleg B (5/5) Oct 03 2019 Hello all
- Adam D. Ruppe (8/10) Oct 03 2019 Indeed, the spec allows the compiler to issue an error, but
- Daniel Kozak (24/29) Oct 03 2019 when you change it a litle it seems works ok:
Hello all I think this code must get compilation error, but it didn't, furthermore result program get UB. https://run.dlang.io/is/Qpr278 Is this correct behavior and why?
Oct 03 2019
On Thursday, 3 October 2019 at 11:56:55 UTC, Oleg B wrote:I think this code must get compilation error, but it didn't, furthermore result program get UB.Indeed, the spec allows the compiler to issue an error, but doesn't require it. tbh it was due to difficulty in implementing the compile time error back when it was designed, so Walter kept the spec flexible. If you mark the function as safe now, you will get a compile error on it now. But without that annotation, the compiler keeps its older, looser behavior.
Oct 03 2019
On Thu, Oct 3, 2019 at 2:00 PM Oleg B via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:Hello all I think this code must get compilation error, but it didn't, furthermore result program get UB. https://run.dlang.io/is/Qpr278 Is this correct behavior and why?when you change it a litle it seems works ok: import std.stdio; struct Foo { int delegate() dg; void foo(return scope int delegate() dg) { this.dg = dg; } int call() { return dg(); } } Foo getFoo() { int x = 42; Foo ret; ret.foo(() => x); return ret; } void main() { writeln(getFoo().call()); }
Oct 03 2019