Bug 4664 – Lambdas default arguments problems

Status
RESOLVED
Resolution
DUPLICATE
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
x86
OS
Windows
Creation time
2010-08-17T07:15:00Z
Last change time
2011-06-08T07:30:12Z
Assigned to
nobody
Creator
bearophile_hugs

Comments

Comment #0 by bearophile_hugs — 2010-08-17T07:15:28Z
In C++0x default arguments are not allowed in lambdas. In D2 they are allowed, but this program shows (compiled with dmd 2.048) that they have problems: import std.stdio: writeln; void foo(T)(T callme) { callme(); } void main() { auto lam1 = (int x=5){ writeln(x); }; auto lam2 = (int x=10){ writeln(x); }; foo(lam1); foo(lam2); } Output: 5 5 Is it positive to disallow default arguments in D2 lambdas too?
Comment #1 by issues.dlang — 2010-08-17T16:42:10Z
I would think that it would be better if the two didn't conflict as opposed to getting rid of default parameters. It looks like they're being turned into the same function when they're not (at least conceptually) - one because they have different default parameters, and two because they're lambdas, not functions with names. Now, assuming that it can't be fixed so that these functions are actually separate like you'd think that they'd be, then it would probably be better to disallow default arguments, but I'd argument that this is a compiler bug rather than a bug in the language.
Comment #2 by nfxjfg — 2010-08-17T16:59:15Z
Is this a dupe of bug #4028? Short story: dmd merges the two delegate types into one, and the first default value "wins".
Comment #3 by issues.dlang — 2010-08-17T19:12:50Z
It does look like it's essentially a duplicate, though the way that the problem is hit is a bit different.
Comment #4 by andrej.mitrovich — 2010-08-30T06:35:48Z
As a quick hack while this isn't fixed you can use this: auto lam1 = function (int x=5){ writeln(x); }; auto lam2 = delegate (int x=10){ writeln(x); }; But this will only work with 2 lambdas at most, and it isn't very nice. :)
Comment #5 by yebblies — 2011-06-08T07:30:12Z
*** This issue has been marked as a duplicate of issue 4028 ***