Skip to content

Commit 5ddd708

Browse files
authored
[spec/expression] Improve IsExpression docs (#3564)
Make it clear that an IsExpression checks a **type**. Add typeof link. Make examples runnable. Expand simple examples.
1 parent 179b285 commit 5ddd708

File tree

1 file changed

+44
-41
lines changed

1 file changed

+44
-41
lines changed

spec/expression.dd

Lines changed: 44 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2688,18 +2688,19 @@ $(GNAME TypeSpecialization):
26882688
$(D package)
26892689
)
26902690

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:)
26932694
$(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)
26982700
)
26992701
$(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.
27032704
)
27042705

27052706
$(P $(I Type) is the type being tested. It must be syntactically
@@ -2711,7 +2712,8 @@ $(GNAME TypeSpecialization):
27112712
pattern matched against.
27122713
)
27132714

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
27152717
whether an expression type checks correctly. For example, $(D is(typeof(foo)))
27162718
will return $(D true) if $(D foo) has a valid type.
27172719
)
@@ -2721,24 +2723,31 @@ $(H4 $(LNAME2 basic-forms, Basic Forms))
27212723
$(H5 $(LNAME2 is-type, $(D is $(LPAREN)) $(I Type) $(D $(RPAREN))))
27222724

27232725
$(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.
27262728
)
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");
27362729

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
27402737
-------------
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
27412745

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+
)
27422751
$(H5 $(LNAME2 is-type-convert, $(D is $(LPAREN)) $(I Type) $(D :) $(I TypeSpecialization) $(D $(RPAREN))))
27432752

27442753
$(P
@@ -2747,36 +2756,29 @@ void foo()
27472756
or can be implicitly converted to $(I TypeSpecialization).
27482757
$(I TypeSpecialization) is only allowed to be a $(I Type).
27492758
)
2759+
2760+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
27502761
-------------
27512762
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));
27602765
-------------
2761-
2766+
)
27622767
$(H5 $(LNAME2 is-type-equal, $(D is $(LPAREN)) $(I Type) $(D ==) $(I TypeSpecialization) $(D $(RPAREN))))
27632768

27642769
$(P
27652770
If *TypeSpecialization* is a type,
27662771
the condition is satisfied if $(I Type) is semantically correct and is
27672772
the same type as $(I TypeSpecialization).
27682773
)
2774+
2775+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
27692776
-------------
27702777
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));
27792780
-------------
2781+
)
27802782
$(P
27812783
If $(I TypeSpecialization) is one of
27822784
$(D struct)
@@ -2795,6 +2797,7 @@ $(SPEC_RUNNABLE_EXAMPLE_COMPILE
27952797
---
27962798
static assert(is(Object == class));
27972799
static assert(is(ModuleInfo == struct));
2800+
static assert(!is(int == class));
27982801
---
27992802
)
28002803
$(P The `module` and `package` forms are satisfied when *Type* is a symbol, not a *type*,

0 commit comments

Comments
 (0)