From issue 19828: import std.stdio : writeln; unittest { int[] arr = [0, 1]; // Different writeln(__traits(isSame, i => arr[i], a => a) ? "Same" : "Different"); // Same test!(i => arr[i], a => a); // Different test!(i => arr[i], a => a * 4); } auto test(alias map1, alias map2)() { writeln(__traits(isSame, map1, map2) ? "Same" : "Different"); }
Two observations: 1) This prints "true" even though the two lambdas are clearly not the same: pragma(msg, __traits(isSame, i => x[i], a => a)); This is a bug in itself. 2) In the original code, when I move `arr` to global scope, then all writelns correctly print "Different". This suggests that, in the context of `test` and __traits(isSame, ...), map1 might be considered to not have access to arr.
looks like a 2.079.z regression : https://run.dlang.io/is/LCV9pX
Not really - lambda comparison was implemented in 2.079. Before that any comparison of lambdas with isSame would return false. Anyways, the core issue is `x` doesn't impact the serialization of lambdas, while it should render the lambda incomparable. Fixed here: https://github.com/dlang/dmd/pull/9710
@Biotronic created dlang/dmd pull request #9710 "Fix issue 19829 - __traits(isSame) returns true for some non-local de…" fixing this issue: - Fix issue 19829 - __traits(isSame) returns true for some non-local delegate lambdas even when they are different https://github.com/dlang/dmd/pull/9710
dlang/dmd pull request #9710 "Fix issue 19829 - __traits(isSame) returns true for some non-local de…" was merged into master: - c4782963597ce3e719a10d218a7933b69d2dafac by Biotronic: Fix issue 19829 - __traits(isSame) returns true for some non-local delegate lambdas even when they are different https://github.com/dlang/dmd/pull/9710