@@ -495,6 +495,7 @@ $(H4 The D Way)
495
495
Properties can be get and set using the normal field syntax,
496
496
yet the get and set will invoke methods instead.
497
497
498
+ $(RUNNABLE_EXAMPLE
498
499
------
499
500
class Abc
500
501
{
@@ -507,15 +508,13 @@ class Abc
507
508
private:
508
509
int myprop;
509
510
}
510
- ------
511
-
512
- which is used as:
513
511
514
- ------
515
512
Abc a = new Abc;
516
513
a.property = 3;
517
514
int x = a.property;
515
+ assert(x == 3);
518
516
------
517
+ )
519
518
520
519
Thus, in D a property is treated like it was a simple field name.
521
520
A property can start out actually being a simple field name,
@@ -563,6 +562,8 @@ $(H4 The D Way)
563
562
$(HTTPS dlang.org/spec/template.html#implicit_template_properties,
564
563
Eponymous Templates) - promotion of single template members to the
565
564
enclosing name space:
565
+
566
+ $(RUNNABLE_EXAMPLE_COMPILE
566
567
------
567
568
template factorial(int n)
568
569
{
@@ -574,19 +575,26 @@ template factorial(int n : 1)
574
575
enum factorial = 1;
575
576
}
576
577
577
- void test()
578
+ unittest
578
579
{
580
+ import std.stdio;
579
581
writeln(factorial!(4)); // prints 24
580
582
}
581
583
------
584
+ )
582
585
The template blocks can be made shorter using
583
586
$(HTTPS dlang.org/spec/template.html#variable-template,
584
587
Enum Template) syntax:
588
+
589
+ $(RUNNABLE_EXAMPLE_COMPILE
585
590
------
586
591
enum factorial(int n) = n * .factorial!(n-1);
587
592
588
593
enum factorial(int n : 1) = 1;
594
+
595
+ static assert(factorial!(4) == 24);
589
596
------
597
+ )
590
598
591
599
<hr>$(COMMENT -------------------------------------------- )
592
600
@@ -712,6 +720,7 @@ $(H4 The D Way)
712
720
It compiles quickly, and gives a sensible compile time message
713
721
if it fails.
714
722
723
+ $(RUNNABLE_EXAMPLE
715
724
------
716
725
import std.stdio;
717
726
@@ -739,6 +748,7 @@ int main()
739
748
return 0;
740
749
}
741
750
------
751
+ )
742
752
743
753
<hr>$(COMMENT -------------------------------------------- )
744
754
@@ -784,6 +794,7 @@ $(H4 The D Way)
784
794
can be done in D without resorting to template argument
785
795
pattern matching:
786
796
797
+ $(RUNNABLE_EXAMPLE_COMPILE
787
798
------
788
799
template IsFunctionT(T)
789
800
{
@@ -793,27 +804,30 @@ template IsFunctionT(T)
793
804
const int IsFunctionT = 1;
794
805
}
795
806
796
- void test()
807
+ unittest
797
808
{
798
809
alias int fp(int);
799
810
800
- assert(IsFunctionT!(fp) == 1);
811
+ static assert(IsFunctionT!(fp) == 1);
801
812
}
802
813
------
814
+ )
803
815
804
816
The task of discovering if a type is a function doesn't need a
805
817
template at all, nor does it need the subterfuge of attempting to
806
818
create the invalid array of functions type.
807
819
The $(ISEXPRESSION) expression can test it directly:
808
820
821
+ $(RUNNABLE_EXAMPLE_COMPILE
809
822
------
810
- void test()
823
+ unittest
811
824
{
812
825
alias int fp(int);
813
826
814
- assert( is(fp == function) );
827
+ static assert( is(fp == function) );
815
828
}
816
829
------
830
+ )
817
831
818
832
$(HR)
819
833
@@ -937,6 +951,8 @@ void main()
937
951
938
952
------
939
953
auto d2 = &gallery[0];
954
+ d2.id = 1;
955
+ assert(d2.id == gallery[0].id);
940
956
------
941
957
942
958
)
0 commit comments