Skip to content

Commit 63f71d1

Browse files
committed
[spec] Make more examples runnable
Move const attribute after parameters. Rename gc -> GC. Fix calling array delegate elements. Rename 2 eponymous template member functions so they can be called explicitly (this is before the IFTI section). Add *Type Sequence Deduction* subheading. Replace some trivial writefln calls with writeln.
1 parent 53cbbf0 commit 63f71d1

File tree

5 files changed

+56
-22
lines changed

5 files changed

+56
-22
lines changed

spec/class.dd

Lines changed: 8 additions & 3 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

@@ -726,7 +731,7 @@ $(GNAME Destructor):
726731
)
727732

728733
$(P Objects referenced from the data segment never get collected
729-
by the gc.
734+
by the GC.
730735
)
731736

732737
$(H2 $(LNAME2 static-constructor, Static Constructors))

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/garbage.dd

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ struct Foo
288288

289289
$(LI Do not use byte-by-byte memory copies to copy pointer values.
290290
This may result in intermediate conditions where there is
291-
not a valid pointer, and if the gc pauses the thread in such a
291+
not a valid pointer, and if the GC pauses the thread in such a
292292
condition, it can corrupt memory.
293293
Most implementations of $(D memcpy()) will work since the
294294
internal implementation of it does the copy in aligned chunks
@@ -401,7 +401,7 @@ $(H2 $(LNAME2 gc_config, Configuring the Garbage Collector))
401401
$(UL
402402
$(LI disable:0|1 - start disabled)
403403
$(LI profile:0|1 - enable profiling with summary when terminating program)
404-
$(LI gc:conservative|precise|manual - select gc implementation (default = conservative))
404+
$(LI gc:conservative|precise|manual - select GC implementation (default = conservative))
405405
$(LI initReserve:N - initial memory to reserve in MB)
406406
$(LI minPoolSize:N - initial and minimum pool size in MB)
407407
$(LI maxPoolSize:N - maximum pool size in MB)

spec/template.dd

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -360,14 +360,15 @@ $(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
---
364365
import std.stdio;
365366

366367
struct S
367368
{
368-
const void foo(this T)(int i)
369+
void foo(this T)(int i) const
369370
{
370-
writeln(typeid(T));
371+
pragma(msg, T);
371372
}
372373
}
373374

@@ -381,6 +382,7 @@ $(GNAME TemplateThisParameter):
381382
s3.foo(3);
382383
}
383384
---
385+
)
384386

385387
Prints:
386388

@@ -470,6 +472,7 @@ $(GNAME TemplateValueParameterDefault):
470472
associative array literals of template value arguments,
471473
or struct literals of template value arguments.)
472474

475+
$(SPEC_RUNNABLE_EXAMPLE_RUN
473476
-----
474477
template foo(string s)
475478
{
@@ -478,9 +481,11 @@ $(GNAME TemplateValueParameterDefault):
478481

479482
void main()
480483
{
481-
writefln("%s", foo!("hello").bar()); // prints: hello betty
484+
import std.stdio;
485+
writeln(foo!("hello").bar()); // prints: hello betty
482486
}
483487
-----
488+
)
484489

485490
$(P This example of template foo has a value parameter that
486491
is specialized for 10:)
@@ -655,6 +660,7 @@ $(GNAME TemplateAliasParameterDefault):
655660

656661
$(LI Literals
657662

663+
$(SPEC_RUNNABLE_EXAMPLE_RUN
658664
------
659665
template Foo(alias x, alias y)
660666
{
@@ -664,11 +670,12 @@ $(GNAME TemplateAliasParameterDefault):
664670

665671
void main()
666672
{
673+
import std.stdio;
667674
alias foo = Foo!(3, "bar");
668675
writeln(foo.i, foo.s); // prints 3bar
669676
}
670677
------
671-
)
678+
))
672679

673680
$(LI Compile-time values
674681
$(SPEC_RUNNABLE_EXAMPLE_RUN
@@ -777,30 +784,35 @@ $(GNAME TemplateSequenceParameter):
777784
$(P An $(I AliasSeq) can be used as an argument list to instantiate
778785
another template, or as the list of parameters for a function.)
779786

787+
$(SPEC_RUNNABLE_EXAMPLE_RUN
780788
---
789+
import std.stdio;
790+
781791
template print(args...)
782792
{
783-
void print()
793+
void f()
784794
{
785795
writeln("args are ", args); // args is a ValueSeq
786796
}
787797
}
788798

789799
template write(Args...)
790800
{
791-
void write(Args args) // Args is a TypeSeq
792-
// args is a ValueSeq
801+
pragma(msg, Args); // Args is a TypeSeq
802+
803+
void f(Args args) // args is a ValueSeq
793804
{
794805
writeln("args are ", args);
795806
}
796807
}
797808

798809
void main()
799810
{
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
811+
print!(1,'a',6.8).f(); // prints: args are 1a6.8
812+
write!(int, char, double).f(1, 'a', 6.8); // prints: args are 1a6.8
802813
}
803814
---
815+
)
804816

805817
$(P The number of elements in an $(I AliasSeq) can be retrieved with
806818
the $(D .length) property. The $(I n)th element can be retrieved
@@ -812,10 +824,15 @@ $(GNAME TemplateSequenceParameter):
812824
to dynamically change, add, or remove elements either at compile-time or run-time.
813825
)
814826

827+
$(H3 $(LNAME2 typeseq_deduction, Type Sequence Deduction))
828+
815829
$(P Type sequences can be deduced from the trailing parameters
816-
of an implicitly instantiated function template:)
830+
of an $(RELATIVE_LINK2 ifti, implicitly instantiated) function template:)
817831

832+
$(SPEC_RUNNABLE_EXAMPLE_RUN
818833
---
834+
import std.stdio;
835+
819836
template print(T, Args...)
820837
{
821838
void print(T first, Args args)
@@ -831,6 +848,7 @@ $(GNAME TemplateSequenceParameter):
831848
print(1, 'a', 6.8);
832849
}
833850
---
851+
)
834852

835853
prints:
836854

@@ -843,6 +861,7 @@ a
843861
$(P Type sequences can also be deduced from the type of a delegate
844862
or function parameter list passed as a function argument:)
845863

864+
$(SPEC_RUNNABLE_EXAMPLE_RUN
846865
----
847866
import std.stdio;
848867

@@ -864,10 +883,12 @@ a
864883
return x + y + z;
865884
}
866885

886+
import std.stdio;
867887
auto plus_two = partial(&plus, 2);
868-
writefln("%d", plus_two(6, 8)); // prints 16
888+
writeln(plus_two(6, 8)); // prints 16
869889
}
870890
----
891+
)
871892
See also: $(REF partial, std,functional)
872893

873894
$(H3 $(LNAME2 variadic_template_specialization, Specialization))
@@ -1067,6 +1088,7 @@ $(H3 $(LNAME2 ifti, Implicit Function Template Instantiation (IFTI)))
10671088
$(I TemplateArgumentList) is deducible from the types of the
10681089
function arguments:)
10691090

1091+
$(SPEC_RUNNABLE_EXAMPLE_RUN
10701092
----
10711093
T square(T)(T t)
10721094
{
@@ -1075,6 +1097,7 @@ $(H3 $(LNAME2 ifti, Implicit Function Template Instantiation (IFTI)))
10751097

10761098
writefln("The square of %s is %s", 3, square(3)); // T is deduced to be int
10771099
----
1100+
)
10781101

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

0 commit comments

Comments
 (0)