@@ -2294,12 +2294,15 @@ $(GNAME TypeidExpression):
2294
2294
of the dynamic type (i.e. the most derived type).
2295
2295
The $(I Expression) is always executed.)
2296
2296
2297
+ $(SPEC_RUNNABLE_EXAMPLE_COMPILE
2297
2298
---
2298
2299
class A { }
2299
2300
class B : A { }
2300
2301
2301
2302
void main()
2302
2303
{
2304
+ import std.stdio;
2305
+
2303
2306
writeln(typeid(int)); // int
2304
2307
uint i;
2305
2308
writeln(typeid(i++)); // uint
@@ -2309,6 +2312,7 @@ $(GNAME TypeidExpression):
2309
2312
writeln(typeid(typeof(a))); // A
2310
2313
}
2311
2314
---
2315
+ )
2312
2316
2313
2317
$(H3 $(LNAME2 is_expression, IsExpression))
2314
2318
@@ -2518,6 +2522,7 @@ void foo()
2518
2522
dependent on $(I Identifier), the deduced type.
2519
2523
)
2520
2524
2525
+ $(SPEC_RUNNABLE_EXAMPLE_COMPILE
2521
2526
---
2522
2527
alias Bar = int;
2523
2528
@@ -2526,17 +2531,20 @@ void foo()
2526
2531
else
2527
2532
alias S = long;
2528
2533
2529
- writeln(typeid(S)); // prints " int"
2534
+ static assert(is(S == int));
2530
2535
---
2536
+ )
2537
+ $(SPEC_RUNNABLE_EXAMPLE_COMPILE
2531
2538
---
2532
2539
alias Abc = long*;
2533
2540
2534
2541
static if (is(Abc U : U*))
2535
2542
{
2536
2543
U u;
2537
- writeln(typeid(typeof(u))); // prints "long"
2538
2544
}
2545
+ static assert(is(U == long));
2539
2546
---
2547
+ )
2540
2548
2541
2549
$(P
2542
2550
The way the type of $(I Identifier) is determined is analogous
@@ -2554,14 +2562,16 @@ void foo()
2554
2562
dependent on $(I Identifier), the deduced type.
2555
2563
)
2556
2564
2565
+ $(SPEC_RUNNABLE_EXAMPLE_COMPILE
2557
2566
---
2558
2567
alias Bar = short;
2559
2568
2560
2569
static if (is(Bar T == int)) // not satisfied, short is not int
2561
2570
alias S = T;
2562
2571
2563
- alias U = T; // error, T is not defined
2572
+ static assert(!is(T)); // T is not defined
2564
2573
---
2574
+ )
2565
2575
2566
2576
$(P
2567
2577
If $(I TypeSpecialization) is one of
@@ -2621,14 +2631,16 @@ void foo()
2621
2631
$(TROW $(D package), the package)
2622
2632
)
2623
2633
2634
+ $(SPEC_RUNNABLE_EXAMPLE_COMPILE
2624
2635
---
2625
2636
enum E : byte { Emember }
2626
2637
2627
2638
static if (is(E V == enum)) // satisfied, E is an enum
2628
2639
V v; // v is declared to be a byte
2629
2640
2630
- pragma(msg, V); // byte
2641
+ static assert(is(V == byte));
2631
2642
---
2643
+ )
2632
2644
2633
2645
$(H4 $(LNAME2 is-parameter-list, Parameter List Forms))
2634
2646
@@ -2648,21 +2660,21 @@ $(D is $(LPAREN)) $(I Type) $(I Identifier) $(D ==) $(I TypeSpecialization) $(D
2648
2660
2649
2661
$(P $(B Example:) Matching a Template Instantiation))
2650
2662
2651
- $(SPEC_RUNNABLE_EXAMPLE_RUN
2663
+ $(SPEC_RUNNABLE_EXAMPLE_COMPILE
2652
2664
---
2653
2665
struct Tuple(T...)
2654
2666
{
2655
2667
// ...
2656
2668
}
2657
- alias Tup = Tuple!(int, string);
2669
+ alias Tup2 = Tuple!(int, string);
2658
2670
2659
- static if (is(Tup : Template!Args, alias Template, Args...))
2671
+ static if (is(Tup2 : Template!Args, alias Template, Args...))
2660
2672
{
2661
- writeln(__traits(isSame, Template, Tuple)); // true
2662
- writeln(is(Template!(int, string) == Tup)); // true, same struct
2663
- writeln(typeid(Args[0])); // int
2664
- writeln(typeid(Args[1])); // immutable(char)[]
2673
+ static assert(__traits(isSame, Template, Tuple));
2674
+ static assert(is(Template!(int, string) == Tup2)); // same struct
2665
2675
}
2676
+ static assert(is(Args[0] == int));
2677
+ static assert(is(Args[1] == string));
2666
2678
---
2667
2679
)
2668
2680
@@ -2685,13 +2697,13 @@ $(SPEC_RUNNABLE_EXAMPLE_COMPILE
2685
2697
2686
2698
$(P $(B Example:) Matching a Static Array)
2687
2699
2688
- $(SPEC_RUNNABLE_EXAMPLE_RUN
2700
+ $(SPEC_RUNNABLE_EXAMPLE_COMPILE
2689
2701
---
2690
2702
static if (is(int[10] W : W[len], int len))
2691
2703
{
2692
- writeln(typeid(W)); // int
2693
- writeln(len); // 10
2704
+ static assert(len == 10);
2694
2705
}
2706
+ static assert(is(W == int));
2695
2707
2696
2708
// no match, len should be 10
2697
2709
static assert(!is(int[10] X : X[len], int len : 5));
@@ -2731,6 +2743,7 @@ $(GNAME SpecialKeyword):
2731
2743
2732
2744
$(P Example:)
2733
2745
2746
+ $(SPEC_RUNNABLE_EXAMPLE_COMPILE
2734
2747
---
2735
2748
module test;
2736
2749
import std.stdio;
@@ -2751,6 +2764,7 @@ $(GNAME SpecialKeyword):
2751
2764
return 0;
2752
2765
}
2753
2766
---
2767
+ )
2754
2768
2755
2769
$(P Assuming the file was at /example/test.d, this will output:)
2756
2770
0 commit comments