Fun with allMembers

Shin Fujishiro rsinfu at gmail.com
Thu May 14 11:55:55 PDT 2009


Another example of allMembers: Generalized selective import.
--------------------
template isUnaryRealFunction(alias sym)
{
    enum isUnaryRealFunction = is(typeof(sym(0.L)) == real);
}

// import unary real functions from std.math
mixin importOnly!("std.math", isUnaryRealFunction);

// unary real functions are imported
static assert(__traits(compiles, abs, sqrt, sin));

// pow is not since it's a binary function
static assert(!__traits(compiles, pow));
--------------------
Full code: http://codepad.org/lc57IKIz

A template importOnly imports only symbols satisfying a predicate
template. I don't know if it's actually useful or not, but it shows the
potential of D's compile-time reflection. Seems we can do everything!



More information about the Digitalmars-d mailing list