|
60 | 60 | $(LREF isPointer)
|
61 | 61 | $(LREF isSignedInteger)
|
62 | 62 | $(LREF isStaticArray)
|
| 63 | + $(LREF isType) |
63 | 64 | $(LREF isUnsignedInteger)
|
64 | 65 | ))
|
65 | 66 | $(TR $(TD Aggregate Type traits) $(TD
|
@@ -1367,6 +1368,85 @@ enum isPointer(T) = is(T == U*, U);
|
1367 | 1368 | }
|
1368 | 1369 | }
|
1369 | 1370 |
|
| 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 | + |
1370 | 1450 | /++
|
1371 | 1451 | Evaluates to $(D true) if the given type or symbol is an instantiation of
|
1372 | 1452 | the given template.
|
|
0 commit comments