Bug 9426 – [enh] polymorphic lambda should be aliasable
Status
RESOLVED
Resolution
DUPLICATE
Severity
enhancement
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-01-29T16:02:00Z
Last change time
2015-06-09T05:14:47Z
Assigned to
nobody
Creator
code
Comments
Comment #0 by code — 2013-01-29T16:02:07Z
I don't see a compelling reason why we should disallow assigning polymorphic lambdas to manifest constants.
This would allow the following code to work.
enum foo = bar => bar * 2;
foo(10);
foo!uint(10); // probably too
The equivalent template would be.
template foo(T) { auto foo(T bar) { return bar * 2; } }
This is for example very handy to replace C-style macros.
#define MASK(val) ((val) & 0xF)
vs.
enum MASK = val => val & 0xF;
related: Bug 7176, Bug 8452
Comment #1 by yebblies — 2013-06-20T02:15:06Z
This would mean 'enum' variable could refer to templates... I don't think this is a good idea, enums are for values.
Alternative:
alias foo = bar => bar * 2;
foo(10);
foo!uint(10); // probably too
This fits much better with the existing lanuage IMO.
Comment #2 by code — 2013-06-20T12:33:49Z
(In reply to comment #1)
> This fits much better with the existing lanuage IMO.
You are right.
Comment #3 by bearophile_hugs — 2013-07-31T05:49:42Z
Now the D language supports enum and alias used as lambdas, so adding template function lambdas seems good:
import std.typecons: Tuple;
void main() {
alias Pair(T) = Tuple!(T, T); // OK
auto p = Pair!int(10, 20);
enum isGood(T) = is(T == int) || is(T == long); // OK
static assert(isGood!int);
enum foo(T) = (T x) => x * x; // OK
static assert(foo!int(5) == 25);
alias bar = x => x * x; // Error
}
Comment #4 by code — 2014-07-04T22:59:28Z
*** This issue has been marked as a duplicate of issue 12421 ***