[Issue 9065] Please consider adding these std.traits
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Mon Nov 26 06:14:49 PST 2012
http://d.puremagic.com/issues/show_bug.cgi?id=9065
--- Comment #11 from Manu <turkeyman at gmail.com> 2012-11-26 06:14:46 PST ---
Okay, I've worked my templates a little more, consider these:
// test if something is a type
template isType( alias T )
{
enum isType = is( T );
}
template isType( T )
{
enum isType = is( T );
}
// test if something is a function
// wrap this up, 'function' is an overloaded term, confusing for newbies
template isFunction( alias T )
{
enum isFunction = is( typeof( T ) == function );
}
template isFunction( T )
{
enum isFunction = is( T == function );
}
// test if something is an enum (used by isVariable)
template isEnum( alias T )
{
template knownAtCompileTime( alias T )
{
// enum knownAtCompileTime = is( typeof( { enum e = T; } ) ); //
immutable breaks this check
enum knownAtCompileTime = !__traits(compiles, ( ref typeof( T ) x ) {}(
T ) ); // hack to see if we can pass it by ref
}
enum isEnum = is( typeof( T ) == enum ) || knownAtCompileTime!T;
}
template isEnum( T )
{
enum isEnum = is( T == enum );
}
// test if something is a variable; has a value, has an address
template isVariable( alias T )
{
enum isVariable = !is( T ) && is( typeof( T ) ) // if it is not a type,
and does have a type, it starts to look like a variable
&& !isFunction!T // reject function
definitions, they can't be assigned to
&& !isEnum!T // reject enum's
&& !is( typeof( T ) == void ); // reject modules, which
appear as a variable of type 'void'
}
template isVariable( T )
{
enum isVariable = false; // types aren't variables
}
This seems to be working for me now (caught a few more edge cases that would
fail on occasion). I'd really like to see all of these in std.traits.
It would be ideal if is() was a rare occurrence, it produces bracket spam, and
most I work with barely understand it past expressions like: is(T == 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