www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 1816] New: Parameter names not visible in return type in function declarations

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

           Summary: Parameter names not visible in return type in function
                    declarations
           Product: D
           Version: unspecified
          Platform: PC
        OS/Version: Linux
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla digitalmars.com
        ReportedBy: andrei metalanguage.com


The following code should compile:

typeof(a) identity(T)(T a) { return a; }

void main()
{
    auto x = identity(1);
}

In this case it's trivial to replace typeof(a) with T, but in the general case
(e.g. mixins, complex expressions) it is necessary to access parameter names in
complex expressions inside typeof().


-- 
Feb 04 2008
next sibling parent downs <default_357-line yahoo.de> writes:
d-bugmail puremagic.com wrote:
 http://d.puremagic.com/issues/show_bug.cgi?id=1816
 
            Summary: Parameter names not visible in return type in function
                     declarations
            Product: D
            Version: unspecified
           Platform: PC
         OS/Version: Linux
             Status: NEW
           Severity: normal
           Priority: P2
          Component: DMD
         AssignedTo: bugzilla digitalmars.com
         ReportedBy: andrei metalanguage.com
 
 
 The following code should compile:
 
 typeof(a) identity(T)(T a) { return a; }
 
 void main()
 {
     auto x = identity(1);
 }
 
 In this case it's trivial to replace typeof(a) with T, but in the general case
 (e.g. mixins, complex expressions) it is necessary to access parameter names in
 complex expressions inside typeof().
 
 
Here's a hack to work around that: either replace your parameter names with Init!(type) where Init == template Init(T) { T Init; } or use the return value of a delegate literal created on the spot. Example: typeof({ T a; return operationOnA(a); }) somefunc(T)(T a) { ... } I use that technique in scrapple.tools to say "if this string was our function body, what would its return type be?" It's fun! :D --downs
Feb 05 2008
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=1816


andrei metalanguage.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |ASSIGNED





Thanks, downs, worked great. You forgot a pair of parens, so for others'
reference here's the current implementation of binaryPred in std.functional:

template binaryPred(string comp) {
    //    BUG   : typeof(mixin(comp)) should work
    typeof({ ElementType a, b; return mixin(comp);}())
    binaryPred(ElementType)(ElementType a, ElementType b)
    {
        return mixin(comp);
    }
}

unittest
{
    alias binaryPred!(q{a < b}) less;
    assert(less(1, 2) && !less(2, 1));
    assert(less("1", "2") && !less("2", "1"));
}


-- 
Feb 05 2008
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=1816


bugzilla digitalmars.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|normal                      |enhancement




-- 
Mar 04 2008