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

Created an attachment (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

 - 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;

    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;
        enum isCallable = false;

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

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

    static assert (!isCallable!int);

Configure issuemail:
------- You are receiving this mail because: -------

Reply via email to