Comment #0 by bradley.chatha — 2020-11-11T03:28:31Z
The following code produces an error (possible bug?) with a very confusing and unclear message:
struct S
{
bool delegate(string) f;
}
@S(str => true) // VALID
int a;
S s = S(str => true); // VALID
struct S2
{
@S(str => false) // INVALID
int a;
}
// Error: delegate onlineapp.S2.__lambda2 cannot be struct members
Comment #1 by bradley.chatha — 2020-11-11T03:32:25Z
Relevant quote from the forums: "The actual problem is that the compiler isn't able to figure out the type of the lambda you've provided. If you change the argument to `(string str)`, it'll work.
The real question is, why does type inference fail for the UDA when it works for the normal constructor call?"
Comment #2 by bradley.chatha — 2021-01-28T17:28:41Z
I have a sneaking suspicion that this may be slightly related to one of the underlying problems for https://issues.dlang.org/show_bug.cgi?id=21496 which is where templated types that contain a template parameter into a struct member, will then implicitly become embedded within the struct itself.
So the anonymous lambda inside of `S2` in the example is being "falsely" embedded into `S2` which is causing this error, likely because the lambda is lowered into a form of template.
Comment #3 by robert.schadek — 2024-12-13T19:12:38Z