[Issue 3197] New: Minor fixes and additions to std.traits

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue Jul 21 03:50:48 PDT 2009


http://d.puremagic.com/issues/show_bug.cgi?id=3197

           Summary: Minor fixes and additions to std.traits
           Product: D
           Version: future
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: Phobos
        AssignedTo: nobody at puremagic.com
        ReportedBy: bugzilla at kyllingen.net


Created an attachment (id=430)
 --> (http://d.puremagic.com/issues/attachment.cgi?id=430)
Patch for traits.d (svn rev. 1242)

These are just some small fixes and additions to std.traits that I've found
useful, and I would very much like to see them included in Phobos.

Firstly, I've attached a patch against traits.d (svn rev. 1242) which does the
following:

 - Made ParameterTypeTuple work with structs/classes with opCall.
   Updated doc comment to reflect this and added unittest.

 - I've changed the code of ReturnType so that the "argument has
   no return type" error is emitted instead of just a template
   instantiation failure whenever a struct/class without an opCall
   is given as the argument.


Secondly, here are two things I'd like to see in std.traits:


/** Get the type of the elements of an array.
 *  Example:
 *  ---
 *  int[][] foo;
 *  ElementType!foo bar;    // bar is declared as int (not int[])
 *  ---
 */
template ElementType(alias dg)
{
    alias ElementType!(typeof(dg)) ElementType;
}

/// ditto
template ElementType(T : T[])
{
    alias ElementType!T ElementType;
}

template ElementType(T)
{
    alias T ElementType;
}

unittest
{
    static assert (is(ElementType!(int[]) == int));
    real[][] foo;
    static assert (is(ElementType!foo == real));
}



/** Detect whether T is a callable type, i.e. a function,
 *  a pointer to a function, a delegate, a struct with an opCall,
 *  a pointer to a struct with an opCall, or a class with
 *  an opCall.
 */
template isCallable(T)
{
    static if (is(T == return))
        enum isCallable = true;
    else static if (is(typeof(&T.opCall) == return))
        enum isCallable = true;
    else
        enum isCallable = false;
}

unittest
{
    static assert (isCallable!(int delegate()));

    struct Bar { int opCall() { return 0; } }
    static assert (isCallable!Bar);

    static assert (!isCallable!int);
}

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


More information about the Digitalmars-d-bugs mailing list