Skip to content

Commit 020e611

Browse files
authored
[articles/cpptod] Make some examples runnable (#4205)
Also demonstrate field access of struct pointer (last example).
1 parent 418bf18 commit 020e611

File tree

1 file changed

+25
-9
lines changed

1 file changed

+25
-9
lines changed

articles/cpptod.dd

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,7 @@ $(H4 The D Way)
495495
Properties can be get and set using the normal field syntax,
496496
yet the get and set will invoke methods instead.
497497

498+
$(RUNNABLE_EXAMPLE
498499
------
499500
class Abc
500501
{
@@ -507,15 +508,13 @@ class Abc
507508
private:
508509
int myprop;
509510
}
510-
------
511-
512-
which is used as:
513511

514-
------
515512
Abc a = new Abc;
516513
a.property = 3;
517514
int x = a.property;
515+
assert(x == 3);
518516
------
517+
)
519518

520519
Thus, in D a property is treated like it was a simple field name.
521520
A property can start out actually being a simple field name,
@@ -563,6 +562,8 @@ $(H4 The D Way)
563562
$(HTTPS dlang.org/spec/template.html#implicit_template_properties,
564563
Eponymous Templates) - promotion of single template members to the
565564
enclosing name space:
565+
566+
$(RUNNABLE_EXAMPLE_COMPILE
566567
------
567568
template factorial(int n)
568569
{
@@ -574,19 +575,26 @@ template factorial(int n : 1)
574575
enum factorial = 1;
575576
}
576577

577-
void test()
578+
unittest
578579
{
580+
import std.stdio;
579581
writeln(factorial!(4)); // prints 24
580582
}
581583
------
584+
)
582585
The template blocks can be made shorter using
583586
$(HTTPS dlang.org/spec/template.html#variable-template,
584587
Enum Template) syntax:
588+
589+
$(RUNNABLE_EXAMPLE_COMPILE
585590
------
586591
enum factorial(int n) = n * .factorial!(n-1);
587592

588593
enum factorial(int n : 1) = 1;
594+
595+
static assert(factorial!(4) == 24);
589596
------
597+
)
590598

591599
<hr>$(COMMENT -------------------------------------------- )
592600

@@ -712,6 +720,7 @@ $(H4 The D Way)
712720
It compiles quickly, and gives a sensible compile time message
713721
if it fails.
714722

723+
$(RUNNABLE_EXAMPLE
715724
------
716725
import std.stdio;
717726

@@ -739,6 +748,7 @@ int main()
739748
return 0;
740749
}
741750
------
751+
)
742752

743753
<hr>$(COMMENT -------------------------------------------- )
744754

@@ -784,6 +794,7 @@ $(H4 The D Way)
784794
can be done in D without resorting to template argument
785795
pattern matching:
786796

797+
$(RUNNABLE_EXAMPLE_COMPILE
787798
------
788799
template IsFunctionT(T)
789800
{
@@ -793,27 +804,30 @@ template IsFunctionT(T)
793804
const int IsFunctionT = 1;
794805
}
795806

796-
void test()
807+
unittest
797808
{
798809
alias int fp(int);
799810

800-
assert(IsFunctionT!(fp) == 1);
811+
static assert(IsFunctionT!(fp) == 1);
801812
}
802813
------
814+
)
803815

804816
The task of discovering if a type is a function doesn't need a
805817
template at all, nor does it need the subterfuge of attempting to
806818
create the invalid array of functions type.
807819
The $(ISEXPRESSION) expression can test it directly:
808820

821+
$(RUNNABLE_EXAMPLE_COMPILE
809822
------
810-
void test()
823+
unittest
811824
{
812825
alias int fp(int);
813826

814-
assert( is(fp == function) );
827+
static assert( is(fp == function) );
815828
}
816829
------
830+
)
817831

818832
$(HR)
819833

@@ -937,6 +951,8 @@ void main()
937951

938952
------
939953
auto d2 = &gallery[0];
954+
d2.id = 1;
955+
assert(d2.id == gallery[0].id);
940956
------
941957

942958
)

0 commit comments

Comments
 (0)