On Sunday, 4 April 2021 at 16:38:10 UTC, frame wrote:
On Saturday, 3 April 2021 at 13:46:17 UTC, kdevel wrote:
Why does this code

   ec.opAssign (bar (1)); // okay
// ec = bar (1); // Error: expression bar(1) is void and has no value

compile with the abovementioned error?

You cannot assign void returned from bar() as parameter to opAssign(). The lazy keyword creates some internal delegate, thus opAssign() works instead.

Thus, the solution is to use an explicit `delegate` instead of `lazy`:

```D
import std.stdio,std.typecons;

struct EC {
   Exception [] ex;
   auto opAssign (X: void delegate()) (scope X f)
   {
      writeln (__PRETTY_FUNCTION__);
      try return f (); catch (Exception e) ex ~= e;
   }
}

class E : Exception { this (string s) { super (s); } }
auto bar (int i) {
    return () {
        if (i == 1)
            throw new E ("E");
    };
}

void main ()
{
   EC ec;

   ec = bar (1); // okay

   ec.writeln;
}
```

Reply via email to