www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 3197] New: Minor fixes and additions to std.traits

reply d-bugmail puremagic.com writes:
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 puremagic.com
        ReportedBy: bugzilla 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: -------
Jul 21 2009
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3197


Andrei Alexandrescu <andrei metalanguage.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |ASSIGNED
                 CC|                            |andrei metalanguage.com
         AssignedTo|nobody puremagic.com        |andrei metalanguage.com





08:38:22 PDT ---
Thanks!

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jul 21 2009
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3197


Andrei Alexandrescu <andrei metalanguage.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|ASSIGNED                    |RESOLVED
         Resolution|                            |FIXED





08:53:09 PDT ---
Integrated patch, thanks.

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