Throwing from a lambda isn't supported by the compiler

Boris Carvajal boris2.9 at gmail.com
Mon Sep 9 09:34:45 UTC 2019


On Monday, 9 September 2019 at 09:14:08 UTC, Andre Pany wrote:
> Hi,
>
> I noticed, that I cannot throw from the lambda expression:
>   Error: expression expected, not throw
> but have to use the longer syntax () {throw new Exception();}
>
> ---
> void foo(void function() e){}
>
> void main()
> {
>     foo(
>         () => throw new Exception()
>     );
> }
> ---
>
> The shorter lambda syntax would be more readable in my case.
> Is there a reason why it isn't allowed or should I create an
> enhancement request?
>
> Kind regards
> André

Well, the shorthand syntax "=>" implies a return, but you can 
only return an expression and throw is a statement.

https://dlang.org/spec/expression.html#lambdas


More information about the Digitalmars-d-learn mailing list