auto & class members

Jonathan M Davis newsgroup.d at jmdavisprog.com
Tue May 22 11:15:08 UTC 2018


On Tuesday, May 22, 2018 10:43:38 Robert M. Münch via Digitalmars-d-learn 
wrote:
> On 2018-05-21 20:17:04 +0000, Jonathan M Davis said:
> > On Monday, May 21, 2018 16:05:00 Steven Schveighoffer via Digitalmars-d-
> >
> > learn wrote:
> >> Well one thing that seems clear from this example -- we now have
> >> __traits(isSame) to tell if lambdas are the same, but it looks like the
> >> compiler doesn't subscribe to that belief...
> >>
> >> https://run.dlang.io/is/FW3mVq
> >>
> >> We should fix that...
> >
> > Yeah. That part of lambdas has always been a problem. My guess here is
> > that the problem stems from the fact that they're declared in separate
> > scopes, but I don't know. Regardless of the reason for the failure
> > though, that example really needs to work, or most anything that cares
> > about lambdas being the same is going to have problems.
>
> I think that's exactly the problem: I assumed that it's about the
> lambdas and associated types but would have never guessed that names,
> scope etc. play a role as well. Is this somewhere documented? Or at
> least a hint, would help a lot to be aware of this pitfall.

The issue is that you actually have different lambdas. They _look_ the same,
but they aren't actually the same function. The compiler is not good about
recognizing that two lambdas are identical. When Steven said that they had
different names, he meant that it was like if you had declared:

int foo(int i)
{
    return i + 42;
}

int bar(int i)
{
    return i + 42;
}

Both functions are identical, but the compiler doesn't see that. It just
looks at the signatures - and while everything about them is functionally
equivalent, they have different names. Basically, the compiler is too dumb
to figure out when two lambdas are actually identical. Some work has been
done towards making it that smart, but there is still clearly more work to
be done. As for how well any of this is documented, I don't know, but I
suspect that at most, the spec has a line or two about it somewhere,
especially since it's really not something that was planned for per se. It's
just a natural fallout of how lambdas work and is surprisingly difficult to
fix.

- Jonathan M Davis




More information about the Digitalmars-d-learn mailing list