Skip to content

Commit 499e4f2

Browse files
authored
Merge pull request #3167 from ntrel/spec-ex
[spec] Make more examples runnable Signed-off-by: Dennis <dkorpel@users.noreply.github.com> Merged-on-behalf-of: Dennis <dkorpel@users.noreply.github.com>
2 parents e9edd87 + 897a00f commit 499e4f2

File tree

5 files changed

+52
-22
lines changed

5 files changed

+52
-22
lines changed

spec/class.dd

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,11 +181,13 @@ $(H2 $(LNAME2 class_properties, Class Properties))
181181
`.tupleof` is not available for `extern(Objective-C)` classes due to
182182
their fields having a dynamic offset.
183183
)
184+
$(SPEC_RUNNABLE_EXAMPLE_RUN
184185
---
185186
class Foo { int x; long y; }
186187

187-
void test(Foo foo)
188+
void main()
188189
{
190+
auto foo = new Foo;
189191
import std.stdio;
190192
static assert(typeof(foo.tupleof).stringof == `(int, long)`);
191193

@@ -195,6 +197,7 @@ void test(Foo foo)
195197
write(x); // prints 12
196198
}
197199
---
200+
)
198201

199202
$(P The properties $(D .__vptr) and $(D .__monitor) give access
200203
to the class object's vtbl[] and monitor, respectively, but
@@ -219,11 +222,12 @@ $(H2 $(LNAME2 member-functions, Member Functions (a.k.a. Methods)))
219222
$(D const), $(D immutable), $(D shared), or $(D inout).
220223
These attributes apply to the hidden $(I this) parameter.
221224
)
225+
$(SPEC_RUNNABLE_EXAMPLE_FAIL
222226
---
223227
class C
224228
{
225229
int a;
226-
const void foo()
230+
void foo() const
227231
{
228232
a = 3; // error, 'this' is const
229233
}
@@ -233,6 +237,7 @@ class C
233237
}
234238
}
235239
---
240+
)
236241

237242
$(H3 $(LNAME2 objc-member-functions, Objective-C linkage))
238243

spec/declaration.dd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,7 @@ b = 4; // sets `S.j` to `4`
434434
$(P Aliases can be used to call a function with different default
435435
arguments, change an argument from required to default or vice versa:)
436436

437+
$(SPEC_RUNNABLE_EXAMPLE_RUN
437438
-----------
438439
import std.stdio : writefln;
439440

@@ -464,6 +465,7 @@ void barbar(int a, int b = 6, int c = 7) {
464465
writefln("a: %d, b: %d, c: %d", a, b, c);
465466
}
466467
-----------
468+
)
467469

468470
$(H2 $(LNAME2 AliasAssign, Alias Assign))
469471

spec/function.dd

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2377,6 +2377,7 @@ $(H4 $(LNAME2 lazy_variadic_functions, Lazy Variadic Functions))
23772377
would evaluate every array element.
23782378
With the latter, only the element being accessed would be evaluated.)
23792379

2380+
$(SPEC_RUNNABLE_EXAMPLE_RUN
23802381
---
23812382
import std.stdio;
23822383

@@ -2399,10 +2400,11 @@ $(H4 $(LNAME2 lazy_variadic_functions, Lazy Variadic Functions))
23992400
// variadic delegate array
24002401
void flash(scope int delegate()[] arr ...)
24012402
{
2402-
writeln(arr[0]); // 1
2403-
writeln(arr[1]); // 2
2403+
writeln(arr[0]()); // 1
2404+
writeln(arr[1]()); // 2
24042405
}
24052406
---
2407+
)
24062408

24072409
$(BEST_PRACTICE Use `scope` when declaring the array of delegates
24082410
parameter. This will prevent a closure being generated for the delegate,
@@ -3020,20 +3022,22 @@ $(H2 $(LNAME2 interpretation, Compile Time Function Execution (CTFE)))
30203022
$(LI $(DDLINK spec/traits, Traits, `__traits` argument))
30213023
)
30223024

3023-
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
3025+
$(SPEC_RUNNABLE_EXAMPLE_RUN
30243026
---
3025-
enum eval(Args...) = Args[0];
3027+
enum eval(alias arg) = arg;
30263028

30273029
int square(int i)
30283030
{
30293031
return i * i;
30303032
}
30313033

3032-
void foo()
3034+
void main()
30333035
{
3036+
import std.stdio;
3037+
30343038
static j = square(3); // CTFE
30353039
writeln(j);
3036-
assert(square(4)); // run time
3040+
assert(square(4) == 16); // run time
30373041
static assert(square(3) == 9); // CTFE
30383042
writeln(eval!(square(5))); // CTFE
30393043
}

spec/operatoroverloading.dd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,6 +1057,7 @@ $(H2 $(LEGACY_LNAME2 Dispatch, dispatch, Forwarding))
10571057
to a template function named $(CODE opDispatch) for resolution.
10581058
)
10591059

1060+
$(SPEC_RUNNABLE_EXAMPLE_RUN
10601061
---
10611062
import std.stdio;
10621063

@@ -1098,6 +1099,7 @@ void main()
10981099
assert(d.foo == 8);
10991100
}
11001101
---
1102+
)
11011103

11021104
$(H2 $(LEGACY_LNAME2 Old-Style, old-style, D1 style operator overloading))
11031105

spec/template.dd

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -360,14 +360,13 @@ $(GNAME TemplateThisParameter):
360360
infer the mutability of the `this` reference. For example, if
361361
`this` is `const`, then the function is marked `const`.)
362362

363+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
363364
---
364-
import std.stdio;
365-
366365
struct S
367366
{
368-
const void foo(this T)(int i)
367+
void foo(this T)(int i) const
369368
{
370-
writeln(typeid(T));
369+
pragma(msg, T);
371370
}
372371
}
373372

@@ -381,6 +380,7 @@ $(GNAME TemplateThisParameter):
381380
s3.foo(3);
382381
}
383382
---
383+
)
384384

385385
Prints:
386386

@@ -470,6 +470,7 @@ $(GNAME TemplateValueParameterDefault):
470470
associative array literals of template value arguments,
471471
or struct literals of template value arguments.)
472472

473+
$(SPEC_RUNNABLE_EXAMPLE_RUN
473474
-----
474475
template foo(string s)
475476
{
@@ -478,9 +479,11 @@ $(GNAME TemplateValueParameterDefault):
478479

479480
void main()
480481
{
481-
writefln("%s", foo!("hello").bar()); // prints: hello betty
482+
import std.stdio;
483+
writeln(foo!("hello").bar()); // prints: hello betty
482484
}
483485
-----
486+
)
484487

485488
$(P This example of template foo has a value parameter that
486489
is specialized for 10:)
@@ -655,6 +658,7 @@ $(GNAME TemplateAliasParameterDefault):
655658

656659
$(LI Literals
657660

661+
$(SPEC_RUNNABLE_EXAMPLE_RUN
658662
------
659663
template Foo(alias x, alias y)
660664
{
@@ -664,11 +668,12 @@ $(GNAME TemplateAliasParameterDefault):
664668

665669
void main()
666670
{
671+
import std.stdio;
667672
alias foo = Foo!(3, "bar");
668673
writeln(foo.i, foo.s); // prints 3bar
669674
}
670675
------
671-
)
676+
))
672677

673678
$(LI Compile-time values
674679
$(SPEC_RUNNABLE_EXAMPLE_RUN
@@ -777,30 +782,33 @@ $(GNAME TemplateSequenceParameter):
777782
$(P An $(I AliasSeq) can be used as an argument list to instantiate
778783
another template, or as the list of parameters for a function.)
779784

785+
$(SPEC_RUNNABLE_EXAMPLE_RUN
780786
---
787+
import std.stdio;
788+
781789
template print(args...)
782790
{
783-
void print()
791+
void f()
784792
{
785793
writeln("args are ", args); // args is a ValueSeq
786794
}
787795
}
788796

789-
template write(Args...)
797+
template write(Args...) // Args is a TypeSeq
790798
{
791-
void write(Args args) // Args is a TypeSeq
792-
// args is a ValueSeq
799+
void f(Args args) // args is a ValueSeq
793800
{
794801
writeln("args are ", args);
795802
}
796803
}
797804

798805
void main()
799806
{
800-
print!(1,'a',6.8).print(); // prints: args are 1a6.8
801-
write!(int, char, double).write(1, 'a', 6.8); // prints: args are 1a6.8
807+
print!(1,'a',6.8).f(); // prints: args are 1a6.8
808+
write!(int, char, double).f(1, 'a', 6.8); // prints: args are 1a6.8
802809
}
803810
---
811+
)
804812

805813
$(P The number of elements in an $(I AliasSeq) can be retrieved with
806814
the $(D .length) property. The $(I n)th element can be retrieved
@@ -813,9 +821,12 @@ $(GNAME TemplateSequenceParameter):
813821
)
814822

815823
$(P Type sequences can be deduced from the trailing parameters
816-
of an implicitly instantiated function template:)
824+
of an $(RELATIVE_LINK2 ifti, implicitly instantiated) function template:)
817825

826+
$(SPEC_RUNNABLE_EXAMPLE_RUN
818827
---
828+
import std.stdio;
829+
819830
template print(T, Args...)
820831
{
821832
void print(T first, Args args)
@@ -831,6 +842,7 @@ $(GNAME TemplateSequenceParameter):
831842
print(1, 'a', 6.8);
832843
}
833844
---
845+
)
834846

835847
prints:
836848

@@ -843,6 +855,7 @@ a
843855
$(P Type sequences can also be deduced from the type of a delegate
844856
or function parameter list passed as a function argument:)
845857

858+
$(SPEC_RUNNABLE_EXAMPLE_RUN
846859
----
847860
import std.stdio;
848861

@@ -864,10 +877,12 @@ a
864877
return x + y + z;
865878
}
866879

880+
import std.stdio;
867881
auto plus_two = partial(&plus, 2);
868-
writefln("%d", plus_two(6, 8)); // prints 16
882+
writeln(plus_two(6, 8)); // prints 16
869883
}
870884
----
885+
)
871886
See also: $(REF partial, std,functional)
872887

873888
$(H3 $(LNAME2 variadic_template_specialization, Specialization))
@@ -1067,6 +1082,7 @@ $(H3 $(LNAME2 ifti, Implicit Function Template Instantiation (IFTI)))
10671082
$(I TemplateArgumentList) is deducible from the types of the
10681083
function arguments:)
10691084

1085+
$(SPEC_RUNNABLE_EXAMPLE_RUN
10701086
----
10711087
T square(T)(T t)
10721088
{
@@ -1075,6 +1091,7 @@ $(H3 $(LNAME2 ifti, Implicit Function Template Instantiation (IFTI)))
10751091

10761092
writefln("The square of %s is %s", 3, square(3)); // T is deduced to be int
10771093
----
1094+
)
10781095

10791096
$(P Type parameter deduction is not influenced by the order of function
10801097
arguments.

0 commit comments

Comments
 (0)