isFunction

Evaluates to $(K_TRUE) if the given type is a function (and to $(K_FALSE) otherwise).

This is equivalent to is(T == function), so most code shouldn't use it. It's intended to be used in conjunction with templates that take a template predicate - such as those in phobos.sys.meta.

Note that this does not include implicit conversions or enum types. The type itself must be a function.

It's not currently possible in D to type out the type of a function on its own, so normally, the only way to get a function type is to get the type of a symbol which is a function. However, ToFunctionType can be used to convert a function pointer type to a function type for code that needs that, though that's likely only to come up in $(K_IS) expressions.

See the documentation for SymbolType and PropertyType to see the quirks involving $(K_PROPERTY) and why either SymbolType or PropertyType should be used rather than $(K_TYPEOF) to get the type of a symbol.

enum isFunction (
T
)

Examples

1 int var;
2 static assert(!isFunction!(SymbolType!var));
3 static assert(!isFunction!(PropertyType!var));
4 static assert(!isFunction!(typeof(&var)));
5 static assert(!is(SymbolType!var == function));
6 static assert(!is(PropertType!var == function));
7 static assert(!is(typeof(&var) == function));
8 
9 static void func() {}
10 static assert( isFunction!(SymbolType!func));
11 static assert(!isFunction!(typeof(&func)));
12 static assert( is(SymbolType!func == function));
13 static assert(!is(typeof(&func) == function));
14 
15 void funcWithContext() { ++var; }
16 static assert( isFunction!(SymbolType!funcWithContext));
17 static assert(!isFunction!(typeof(&funcWithContext)));
18 static assert( is(SymbolType!funcWithContext == function));
19 static assert(!is(typeof(&funcWithContext) == function));
20 
21 int function() funcPtr;
22 static assert(!isFunction!(SymbolType!funcPtr));
23 static assert(!isFunction!(PropertyType!funcPtr));
24 static assert(!isFunction!(typeof(&funcPtr)));
25 static assert(!is(SymbolType!funcPtr == function));
26 static assert(!is(PropertyType!funcPtr == function));
27 static assert(!is(typeof(&funcPtr) == function));
28 
29 int delegate() del;
30 static assert(!isFunction!(SymbolType!del));
31 static assert(!isFunction!(PropertyType!del));
32 static assert(!isFunction!(typeof(&del)));
33 static assert(!is(SymbolType!del == function));
34 static assert(!is(PropertyType!del == function));
35 static assert(!is(typeof(&del) == function));
36 
37 // It's possible to get the pointers to the function and the context from a
38 // delegate in @system code.
39 static assert( isFunctionPointer!(typeof(SymbolType!del.funcptr)));
40 static assert( is(typeof(SymbolType!del.ptr) == void*));
41 
42 @property static int prop() { return 0; }
43 static assert( isFunction!(SymbolType!prop));
44 static assert(!isFunction!(PropertyType!prop));
45 static assert(!isFunction!(typeof(&prop)));
46 static assert( is(SymbolType!prop == function));
47 static assert(!is(PropertyType!prop == function));
48 static assert(!is(typeof(&prop) == function));
49 
50 @property int propWithContext() { return var; }
51 static assert( isFunction!(SymbolType!propWithContext));
52 static assert(!isFunction!(PropertyType!propWithContext));
53 static assert(!isFunction!(typeof(&propWithContext)));
54 static assert( is(SymbolType!propWithContext == function));
55 static assert(!is(PropertyType!propWithContext == function));
56 static assert(!is(typeof(&propWithContext) == function));
57 
58 static int function() propFuncPtr() @property { return null; }
59 static assert( isFunction!(SymbolType!propFuncPtr));
60 static assert(!isFunction!(PropertyType!propFuncPtr));
61 static assert(!isFunction!(typeof(&propFuncPtr)));
62 static assert( is(SymbolType!propFuncPtr == function));
63 static assert(!is(PropertyType!propFuncPtr == function));
64 static assert(!is(typeof(&propFuncPtr) == function));
65 
66 static int delegate() propDel() @property { return null; }
67 static assert( isFunction!(SymbolType!propDel));
68 static assert(!isFunction!(PropertyType!propDel));
69 static assert(!isFunction!(typeof(&propDel)));
70 static assert( is(SymbolType!propDel == function));
71 static assert(!is(PropertyType!propDel == function));
72 static assert(!is(typeof(&propDel) == function));
73 
74 static struct S
75 {
76     void foo() {}
77 }
78 static assert( isFunction!(SymbolType!(S.foo)));
79 static assert(!isFunction!(typeof(&S.foo)));
80 static assert(!isFunction!(typeof(&S.init.foo)));
81 static assert( is(SymbolType!(S.foo) == function));
82 static assert(!is(typeof(&S.foo) == function));
83 static assert(!is(typeof(&S.init.foo) == function));
84 
85 struct HasContext
86 {
87     void foo() { ++var; }
88 }
89 static assert( isFunction!(SymbolType!(S.foo)));
90 static assert(!isFunction!(typeof(&S.foo)));
91 static assert(!isFunction!(typeof(&S.init.foo)));
92 static assert( is(SymbolType!(S.foo) == function));
93 static assert(!is(typeof(&S.foo) == function));
94 static assert(!is(typeof(&S.init.foo) == function));

See Also

Meta