Skip to content

Commit 6f138f7

Browse files
ntreldlang-bot
authored andcommitted
[spec/expression] Tweak examples
1 parent 404bac2 commit 6f138f7

File tree

1 file changed

+28
-14
lines changed

1 file changed

+28
-14
lines changed

spec/expression.dd

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2294,12 +2294,15 @@ $(GNAME TypeidExpression):
22942294
of the dynamic type (i.e. the most derived type).
22952295
The $(I Expression) is always executed.)
22962296

2297+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
22972298
---
22982299
class A { }
22992300
class B : A { }
23002301

23012302
void main()
23022303
{
2304+
import std.stdio;
2305+
23032306
writeln(typeid(int)); // int
23042307
uint i;
23052308
writeln(typeid(i++)); // uint
@@ -2309,6 +2312,7 @@ $(GNAME TypeidExpression):
23092312
writeln(typeid(typeof(a))); // A
23102313
}
23112314
---
2315+
)
23122316

23132317
$(H3 $(LNAME2 is_expression, IsExpression))
23142318

@@ -2518,6 +2522,7 @@ void foo()
25182522
dependent on $(I Identifier), the deduced type.
25192523
)
25202524

2525+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
25212526
---
25222527
alias Bar = int;
25232528

@@ -2526,17 +2531,20 @@ void foo()
25262531
else
25272532
alias S = long;
25282533

2529-
writeln(typeid(S)); // prints "int"
2534+
static assert(is(S == int));
25302535
---
2536+
)
2537+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
25312538
---
25322539
alias Abc = long*;
25332540

25342541
static if (is(Abc U : U*))
25352542
{
25362543
U u;
2537-
writeln(typeid(typeof(u))); // prints "long"
25382544
}
2545+
static assert(is(U == long));
25392546
---
2547+
)
25402548

25412549
$(P
25422550
The way the type of $(I Identifier) is determined is analogous
@@ -2554,14 +2562,16 @@ void foo()
25542562
dependent on $(I Identifier), the deduced type.
25552563
)
25562564

2565+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
25572566
---
25582567
alias Bar = short;
25592568

25602569
static if (is(Bar T == int)) // not satisfied, short is not int
25612570
alias S = T;
25622571

2563-
alias U = T; // error, T is not defined
2572+
static assert(!is(T)); // T is not defined
25642573
---
2574+
)
25652575

25662576
$(P
25672577
If $(I TypeSpecialization) is one of
@@ -2621,14 +2631,16 @@ void foo()
26212631
$(TROW $(D package), the package)
26222632
)
26232633

2634+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
26242635
---
26252636
enum E : byte { Emember }
26262637

26272638
static if (is(E V == enum)) // satisfied, E is an enum
26282639
V v; // v is declared to be a byte
26292640

2630-
pragma(msg, V); // byte
2641+
static assert(is(V == byte));
26312642
---
2643+
)
26322644

26332645
$(H4 $(LNAME2 is-parameter-list, Parameter List Forms))
26342646

@@ -2648,21 +2660,21 @@ $(D is $(LPAREN)) $(I Type) $(I Identifier) $(D ==) $(I TypeSpecialization) $(D
26482660

26492661
$(P $(B Example:) Matching a Template Instantiation))
26502662

2651-
$(SPEC_RUNNABLE_EXAMPLE_RUN
2663+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
26522664
---
26532665
struct Tuple(T...)
26542666
{
26552667
// ...
26562668
}
2657-
alias Tup = Tuple!(int, string);
2669+
alias Tup2 = Tuple!(int, string);
26582670

2659-
static if (is(Tup : Template!Args, alias Template, Args...))
2671+
static if (is(Tup2 : Template!Args, alias Template, Args...))
26602672
{
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
26652675
}
2676+
static assert(is(Args[0] == int));
2677+
static assert(is(Args[1] == string));
26662678
---
26672679
)
26682680

@@ -2685,13 +2697,13 @@ $(SPEC_RUNNABLE_EXAMPLE_COMPILE
26852697

26862698
$(P $(B Example:) Matching a Static Array)
26872699

2688-
$(SPEC_RUNNABLE_EXAMPLE_RUN
2700+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
26892701
---
26902702
static if (is(int[10] W : W[len], int len))
26912703
{
2692-
writeln(typeid(W)); // int
2693-
writeln(len); // 10
2704+
static assert(len == 10);
26942705
}
2706+
static assert(is(W == int));
26952707

26962708
// no match, len should be 10
26972709
static assert(!is(int[10] X : X[len], int len : 5));
@@ -2731,6 +2743,7 @@ $(GNAME SpecialKeyword):
27312743

27322744
$(P Example:)
27332745

2746+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
27342747
---
27352748
module test;
27362749
import std.stdio;
@@ -2751,6 +2764,7 @@ $(GNAME SpecialKeyword):
27512764
return 0;
27522765
}
27532766
---
2767+
)
27542768

27552769
$(P Assuming the file was at /example/test.d, this will output:)
27562770

0 commit comments

Comments
 (0)