www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 9426] New: [enh] polymorphic lambda should be storeable in enum constant

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9426

           Summary: [enh] polymorphic lambda should be storeable in enum
                    constant
           Product: D
           Version: unspecified
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: code dawg.eu


--- Comment #0 from Martin Nowak <code dawg.eu> 2013-01-29 16:02:07 PST ---
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

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jan 29 2013
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9426


yebblies <yebblies gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |yebblies gmail.com


--- Comment #1 from yebblies <yebblies gmail.com> 2013-06-20 19:15:06 EST ---
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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jun 20 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9426



--- Comment #2 from Martin Nowak <code dawg.eu> 2013-06-20 12:33:49 PDT ---
(In reply to comment #1)
 This fits much better with the existing lanuage IMO.

You are right. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Jun 20 2013
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9426


bearophile_hugs eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bearophile_hugs eml.cc


--- Comment #3 from bearophile_hugs eml.cc 2013-07-31 05:49:42 PDT ---
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
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jul 31 2013