Skip to content

Commit f243f64

Browse files
authored
Add isType to phobos.sys.traits. (#10663)
This is a simple and straightforward trait. It just says whether the template argument is a type or not. It has the same functionality as the trait with the same name in std.traits. However, the implementation is different. The std.traits version just has an alias overload and uses an is expression, whereas it seemed like it would probably be more efficient to split the trait into two and have the kind of argument determine the result, since the compiler has to go through the work of deciding whether the argument matches already, though I don't know enough about the compiler implementation to know for sure that this implementation is better. Either way, it can be changed later if it's determined that the std.traits implementation is better.
1 parent 3f990a7 commit f243f64

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

phobos/sys/traits.d

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
$(LREF isPointer)
6161
$(LREF isSignedInteger)
6262
$(LREF isStaticArray)
63+
$(LREF isType)
6364
$(LREF isUnsignedInteger)
6465
))
6566
$(TR $(TD Aggregate Type traits) $(TD
@@ -1367,6 +1368,85 @@ enum isPointer(T) = is(T == U*, U);
13671368
}
13681369
}
13691370

1371+
/++
1372+
Evaluates to $(D true) if given a type and $(D false) for all other symbols.
1373+
1374+
This is equivalent to $(D is(T)), but some people may find using a named
1375+
trait to be clearer, and it can be used in conjunction with templates that
1376+
take a template predicate (such as those in phobos.sys.meta), which can't
1377+
be done with naked is expressions.
1378+
1379+
See_Also:
1380+
$(DDSUBLINK dlang.org/spec/expression.html, is-type, Spec on the related is expression)
1381+
+/
1382+
enum isType(T) = true;
1383+
1384+
/// Ditto
1385+
enum isType(alias sym) = false;
1386+
1387+
///
1388+
@safe unittest
1389+
{
1390+
static assert( isType!int);
1391+
static assert( isType!(int[]));
1392+
static assert( isType!string);
1393+
static assert( isType!(int[int]));
1394+
static assert( isType!(ubyte*));
1395+
static assert( isType!void);
1396+
1397+
int i;
1398+
static assert(!isType!i);
1399+
static assert( isType!(typeof(i)));
1400+
1401+
struct S {}
1402+
static assert( isType!S);
1403+
static assert(!isType!(S.init));
1404+
1405+
class C {}
1406+
static assert( isType!C);
1407+
static assert(!isType!(C.init));
1408+
1409+
interface I {}
1410+
static assert( isType!I);
1411+
static assert(!isType!(I.init));
1412+
1413+
union U {}
1414+
static assert( isType!U);
1415+
static assert(!isType!(U.init));
1416+
1417+
static void func() {}
1418+
static assert(!isType!func);
1419+
static assert( isType!(typeof(func)));
1420+
1421+
void funcWithContext() { ++i; }
1422+
static assert(!isType!funcWithContext);
1423+
static assert( isType!(typeof(funcWithContext)));
1424+
1425+
int function() funcPtr;
1426+
static assert(!isType!funcPtr);
1427+
static assert( isType!(typeof(funcPtr)));
1428+
1429+
int delegate() del;
1430+
static assert(!isType!del);
1431+
static assert( isType!(typeof(del)));
1432+
1433+
template Templ() {}
1434+
static assert(!isType!Templ);
1435+
static assert(!isType!(Templ!()));
1436+
1437+
template TemplWithType()
1438+
{
1439+
struct S {}
1440+
}
1441+
static assert(!isType!TemplWithType);
1442+
static assert(!isType!(TemplWithType!()));
1443+
static assert( isType!(TemplWithType!().S));
1444+
1445+
struct TemplType() {}
1446+
static assert(!isType!TemplType);
1447+
static assert( isType!(TemplType!()));
1448+
}
1449+
13701450
/++
13711451
Evaluates to $(D true) if the given type or symbol is an instantiation of
13721452
the given template.

0 commit comments

Comments
 (0)