Structure initializer VS lambda function

Steven Schveighoffer schveiguy at gmail.com
Mon Dec 12 15:36:35 UTC 2022


On 12/12/22 3:54 AM, realhet wrote:
> Hi,
> 
> I'm writing a DLang parser and got confused of this.
> What is a good way to distinguish lambda functions and structure 
> initialization blocks.
> 
> Both of them are {} blocks.
> 
> I'm thinking of something like this:
> 
> 1. checking inside (on the first hierarchy level inside {})
>     ,   => must be a struct initializer
>     ;   => must be a lambda
>     no , and no ;  => check it from the outside
> 
> 
> 2. checking outside (on the same hierarchy level as the {}):
>     () before {}   ->  lambda
>     => before {}   ->  lambda
>     () after {}    ->  lambda  //this check feels wrong to me.
>     otherwise      ->  struct initializer
> 
> But I think it's logically loose.
> I have only the syntax tree, I have no access to semantics. I don't know 
> if an identifier is a struct for example.
> 
> Is there a better way to do this?
> 
> Thank You in advance!

This has actually been discussed recently on discord, I believe the 
difference is if you see a statement inside the braces (e.g. a semicolon).

It's not a great situation, and I think if we removed the ability to do 
lambdas with `{}` without leading parentheses, it could clear this up 
pretty well.

-Steve


More information about the Digitalmars-d-learn mailing list