Detecting if the callee is static
I thought I could do it with std.traits.functionAttributes, but it doesn't support static
. For any type of callable (structures with opCall enabled), how can I determine if this callable code is invalidated with static
? For example:
template isStaticCallable(alias fun) if (isCallable!fun)
{
enum isStaticCallable = ...?
}
+3
source to share
1 answer
Traits are part of dlang that provide compile-time understanding of information. One of the available traits is isStaticFunction
used as __traits(isStaticFunction, fun)
.
Sample code:
import std.traits;
template isStaticCallable(alias fun) if (isCallable!fun)
{
enum isStaticCallable = __traits(isStaticFunction, fun);
}
void main() {}
class Foo
{
static void boo() {}
void zoo() {}
}
pragma(msg, isStaticCallable!main); // note that this prints true because
// the function has no context pointer
pragma(msg, isStaticCallable!(Foo.boo)); // prints true
pragma(msg, isStaticCallable!(Foo.zoo)); // prints false
+4
source to share