Skip to content

Commit 999b129

Browse files
authored
[std.traits] Improve docs and unittest for isFunction (#10904)
Note that `is(X == function)` is only true when X is a function type, so the docs here were misleading. Remove unnecessary `T` identifier in implementation. Add tests.
1 parent 775fd93 commit 999b129

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

std/traits.d

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9143,9 +9143,9 @@ enum isType(alias X) = is(X);
91439143
}
91449144

91459145
/**
9146-
* Detect whether symbol or type `X` is a function. This is different that finding
9147-
* if a symbol is callable or satisfying `is(X == function)`, it finds
9148-
* specifically if the symbol represents a normal function declaration, i.e.
9146+
* Detect whether symbol or type `X` is a function.
9147+
* This is different from finding if a symbol is callable or satisfying `is(X == return)`.
9148+
* It finds specifically if the symbol represents a normal function (or method) declaration, i.e.
91499149
* not a delegate or a function pointer.
91509150
*
91519151
* Returns:
@@ -9163,10 +9163,10 @@ template isFunction(alias X)
91639163
// x is a (nested) function symbol.
91649164
enum isFunction = true;
91659165
}
9166-
else static if (is(X T))
9166+
else static if (is(X))
91679167
{
9168-
// x is a type. Take the type of it and examine.
9169-
enum isFunction = is(T == function);
9168+
// x is a type
9169+
enum isFunction = is(X == function);
91709170
}
91719171
else
91729172
enum isFunction = false;
@@ -9177,6 +9177,14 @@ template isFunction(alias X)
91779177
{
91789178
static void func(){}
91799179
static assert(isFunction!func);
9180+
static assert(isFunction!(typeof(func)));
9181+
9182+
auto fp = &func; // function pointer
9183+
static assert(!isFunction!fp);
9184+
9185+
int i;
9186+
int f2() => i; // nested function
9187+
static assert(isFunction!f2);
91809188

91819189
struct S
91829190
{

0 commit comments

Comments
 (0)