@@ -2688,18 +2688,19 @@ $(GNAME TypeSpecialization):
2688
2688
$(D package)
2689
2689
)
2690
2690
2691
- $(P $(I IsExpression)s are evaluated at compile time and are
2692
- used for:)
2691
+ $(P An $(I IsExpression) is evaluated at compile time and is
2692
+ used to check if an expression is a valid type. In addition,
2693
+ there are forms which can also:)
2693
2694
$(UL
2694
- $(LI checking for valid types)
2695
- $(LI comparing types for equivalence)
2696
- $(LI determining if one type can be implicitly converted to another)
2697
- $(LI deducing the subtypes of a type using pattern matching)
2695
+ $(LI compare types for equivalence)
2696
+ $(LI determine if one type can be implicitly converted to another)
2697
+ $(LI deduce the subtypes of a type using
2698
+ $(RELATIVE_LINK2 is-parameter-list, pattern matching))
2699
+ $(LI deduce the template arguments of a type template instance)
2698
2700
)
2699
2701
$(P
2700
- The result of an $(I IsExpression) is a boolean of value `true`
2701
- if the condition is satisfied. If the condition is not satisfied,
2702
- the result is a boolean of value `false`.
2702
+ The result of an $(I IsExpression) is a boolean which is `true`
2703
+ if the condition is satisfied and `false` if not.
2703
2704
)
2704
2705
2705
2706
$(P $(I Type) is the type being tested. It must be syntactically
@@ -2711,7 +2712,8 @@ $(GNAME TypeSpecialization):
2711
2712
pattern matched against.
2712
2713
)
2713
2714
2714
- $(P $(I IsExpression)s may be used in conjunction with $(D typeof) to check
2715
+ $(P $(I IsExpression)s may be used in conjunction with
2716
+ $(DDSUBLINK spec/type, typeof, `typeof`) to check
2715
2717
whether an expression type checks correctly. For example, $(D is(typeof(foo)))
2716
2718
will return $(D true) if $(D foo) has a valid type.
2717
2719
)
@@ -2721,24 +2723,31 @@ $(H4 $(LNAME2 basic-forms, Basic Forms))
2721
2723
$(H5 $(LNAME2 is-type, $(D is $(LPAREN)) $(I Type) $(D $(RPAREN))))
2722
2724
2723
2725
$(P
2724
- The condition is satisfied if $(D Type) is semantically
2725
- correct (it must be syntactically correct regardless) .
2726
+ The condition is satisfied if $(I Type) is semantically
2727
+ correct. *Type* must be syntactically correct regardless.
2726
2728
)
2727
- -------------
2728
- alias int Func(int); // Func is a alias to a function type
2729
- void foo()
2730
- {
2731
- if (is(Func[])) // not satisfied because arrays of
2732
- // functions are not allowed
2733
- writeln("satisfied");
2734
- else
2735
- writeln("not satisfied");
2736
2729
2737
- if (is([][])) // error, [][] is not a syntactically valid type
2738
- ...
2739
- }
2730
+ $(SPEC_RUNNABLE_EXAMPLE_FAIL
2731
+ ---
2732
+ pragma(msg, is(5)); // error
2733
+ pragma(msg, is([][])); // error
2734
+ ---
2735
+ )
2736
+ $(SPEC_RUNNABLE_EXAMPLE_COMPILE
2740
2737
-------------
2738
+ int i;
2739
+ static assert(is(int));
2740
+ static assert(is(typeof(i))); // same
2741
+
2742
+ static assert(!is(Undefined));
2743
+ static assert(!is(typeof(int))); // int is not an expression
2744
+ static assert(!is(i)); // i is a value
2741
2745
2746
+ alias Func = int(int); // function type
2747
+ static assert(is(Func));
2748
+ static assert(!is(Func[])); // fails as an array of functions is not allowed
2749
+ -------------
2750
+ )
2742
2751
$(H5 $(LNAME2 is-type-convert, $(D is $(LPAREN)) $(I Type) $(D :) $(I TypeSpecialization) $(D $(RPAREN))))
2743
2752
2744
2753
$(P
@@ -2747,36 +2756,29 @@ void foo()
2747
2756
or can be implicitly converted to $(I TypeSpecialization).
2748
2757
$(I TypeSpecialization) is only allowed to be a $(I Type).
2749
2758
)
2759
+
2760
+ $(SPEC_RUNNABLE_EXAMPLE_COMPILE
2750
2761
-------------
2751
2762
alias Bar = short;
2752
- void foo()
2753
- {
2754
- if (is(Bar : int)) // satisfied because short can be
2755
- // implicitly converted to int
2756
- writeln("satisfied");
2757
- else
2758
- writeln("not satisfied");
2759
- }
2763
+ static assert(is(Bar : int)); // short implicitly converts to int
2764
+ static assert(!is(Bar : string));
2760
2765
-------------
2761
-
2766
+ )
2762
2767
$(H5 $(LNAME2 is-type-equal, $(D is $(LPAREN)) $(I Type) $(D ==) $(I TypeSpecialization) $(D $(RPAREN))))
2763
2768
2764
2769
$(P
2765
2770
If *TypeSpecialization* is a type,
2766
2771
the condition is satisfied if $(I Type) is semantically correct and is
2767
2772
the same type as $(I TypeSpecialization).
2768
2773
)
2774
+
2775
+ $(SPEC_RUNNABLE_EXAMPLE_COMPILE
2769
2776
-------------
2770
2777
alias Bar = short;
2771
- void foo()
2772
- {
2773
- if (is(Bar == int)) // not satisfied because short is not
2774
- // the same type as int
2775
- writeln("satisfied");
2776
- else
2777
- writeln("not satisfied");
2778
- }
2778
+ static assert(is(Bar == short));
2779
+ static assert(!is(Bar == int));
2779
2780
-------------
2781
+ )
2780
2782
$(P
2781
2783
If $(I TypeSpecialization) is one of
2782
2784
$(D struct)
@@ -2795,6 +2797,7 @@ $(SPEC_RUNNABLE_EXAMPLE_COMPILE
2795
2797
---
2796
2798
static assert(is(Object == class));
2797
2799
static assert(is(ModuleInfo == struct));
2800
+ static assert(!is(int == class));
2798
2801
---
2799
2802
)
2800
2803
$(P The `module` and `package` forms are satisfied when *Type* is a symbol, not a *type*,
0 commit comments