Thread overview
[Issue 1816] New: Parameter names not visible in return type in function declarations
Feb 05, 2008
d-bugmail
Feb 05, 2008
downs
Feb 05, 2008
d-bugmail
Mar 05, 2008
d-bugmail
February 05, 2008
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().


-- 

February 05, 2008
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
February 05, 2008
http://d.puremagic.com/issues/show_bug.cgi?id=1816


andrei@metalanguage.com changed:

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




------- Comment #2 from andrei@metalanguage.com  2008-02-05 10:09 -------
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"));
}


-- 

March 05, 2008
http://d.puremagic.com/issues/show_bug.cgi?id=1816


bugzilla@digitalmars.com changed:

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




--