Bug 3197 – Minor fixes and additions to std.traits

Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2009-07-21T03:50:00Z
Last change time
2015-06-09T05:13:48Z
Assigned to
andrei
Creator
bugzilla

Attachments

IDFilenameSummaryContent-TypeSize
430traits.d.diffPatch for traits.d (svn rev. 1242)text/plain2205

Comments

Comment #0 by bugzilla — 2009-07-21T03:50:47Z
Created attachment 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); }
Comment #1 by andrei — 2009-07-21T08:38:22Z
Thanks!
Comment #2 by andrei — 2009-08-28T08:53:09Z
Integrated patch, thanks.