forked from dlang/dlang.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
changelog.dd
6322 lines (5797 loc) · 351 KB
/
changelog.dd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
Ddoc
$(D_S D Change Log,
$(COMMENT $(UPCOMING
$(LI Shared libraries for Linux)
))
$(VERSION 063, May 28, 2013, =================================================,
$(BUGSTITLE Language Changes,
$(LI $(RELATIVE_LINK2 staticfields, Const and immutable fields with initializers are now warned about.))
$(LI $(RELATIVE_LINK2 ctorqualifier, Constructor qualifiers are taken into account when constructing objects.))
$(LI $(RELATIVE_LINK2 structuralcompare, Struct members which require non-bitwise comparison are now properly compared.))
$(LI $(RELATIVE_LINK2 slicecopy, Array copy operations now always require using the slice syntax.))
$(LI $(RELATIVE_LINK2 passtype, Types no longer act as arguments in typeof expressions.))
$(LI $(RELATIVE_LINK2 foreachref, The index variable in a foreach range is no longer implicitly a reference.))
$(LI $(RELATIVE_LINK2 hashentry, Associative array entries are no longer default-initialized before assignment.))
$(LI $(RELATIVE_LINK2 constinherit, The const attribute is no longer inherited in overriden methods.))
$(LI $(RELATIVE_LINK2 typeofnullconv, typeof(null) no longer implicitly converts to T[].))
$(LI $(RELATIVE_LINK2 templatethisattrib, The Template This Parameter now changes the member function qualifier.))
$(LI $(RELATIVE_LINK2 sliceref, Array slices are now r-values.))
$(LI $(RELATIVE_LINK2 thisrefaccess, Accessing a non-static field without a $(D this) reference is only allowed in certain contexts.))
$(LI $(RELATIVE_LINK2 implicitarrayptr, Arrays no longer implicitly convert to a pointer.))
)
$(BUGSTITLE Language Enhancements,
$(LI $(RELATIVE_LINK2 uniqueinference, Expressions which return unique objects can be implicitly casted to immutable.))
$(LI $(RELATIVE_LINK2 staticarrayvoid, Static array of void can now be user-initialized.))
$(LI $(RELATIVE_LINK2 multiinvariant, Aggregates can now contain multiple invariants.))
$(LI $(RELATIVE_LINK2 attribinference, Methods of templated aggregates can now infer attributes.))
$(LI $(RELATIVE_LINK2 isexpident, $(B is expression) no longer requires an identifier.))
$(LI $(RELATIVE_LINK2 implicitarraycast, Dynamic arrays of known size can be implicitly cast to static arrays in some contexts.))
$(LI $(RELATIVE_LINK2 tupleinitvoid, Tuples can now be void-initialized.))
$(LI $(RELATIVE_LINK2 tupleequality, Tuples can now be compared for equality.))
$(LI $(RELATIVE_LINK2 templconstraint, Template constraints can now be put after the inheritance list.))
$(LI $(RELATIVE_LINK2 initfield, Fields with initializers can now be re-initialized in a const constructor.))
$(LI $(RELATIVE_LINK2 isnestedtrait, Added the $(B isNested) trait for discovery of aggregates and functions with context pointers.))
$(LI $(RELATIVE_LINK2 nestedtemplate, Templates can now be nested inside of functions.))
$(LI $(RELATIVE_LINK2 ufcslocalimport, UFCS now works with scoped local imports.))
$(LI $(RELATIVE_LINK2 prettyfunc, Added $(D __FUNCTION__), $(D __PRETTY_FUNCTION__) and $(D __MODULE__).))
$(LI $(RELATIVE_LINK2 deprecatedmacro, DDoc: Deprecated declarations are now wrapped in a $(B DEPRECATED) macro.))
$(LI $(RELATIVE_LINK2 documentedunittest, Added documented unittest feature for verifiable code example generation.))
)
$(BUGSTITLE Compiler Enhancements,
$(LI $(RELATIVE_LINK2 mainswitch, Added -main switch which adds an empty main function.))
$(LI $(RELATIVE_LINK2 minimalcov, Added -cov=percentage switch for minimal coverage tests.))
$(LI $(RELATIVE_LINK2 symbolmangle, Added ability to override the mangling of a symbol with a compiler pragma.))
)
$(BUGSTITLE Phobos Changes,
$(LI $(RELATIVE_LINK2 scopedtypeof, std.typecons.scoped implementation changed, potentially breaking some user-code.))
)
$(BUGSTITLE Phobos Enhancements,
$(LI $(RELATIVE_LINK2 newstdprocess, std.process has been redesigned from the ground up and introduces a new API and functionality.))
$(LI $(RELATIVE_LINK2 getoptbool, std.getopt can now set booleans to false.))
$(LI $(RELATIVE_LINK2 ownertid, Added ownerTid property in std.concurrency.))
)
$(BR)$(BIG $(RELATIVE_LINK2 list2063, List of all bug fixes and enhancements in D 2.063.))
$(HR)
$(BUGSTITLE Language Changes,
$(LI $(LNAME2 staticfields, Const and immutable fields with initializers are now warned about:)
$(P Eventually, they will be deprecated, and then will trigger an error. Such fields should now
be changed to enum or static.)
$(P In a future release, a new behavior for them will be enabled:)
$(P Fields in an aggregate which are not static will always be addressable.
This means they will occupy space in the object:)
---------
struct S
{
// used to be implicitly static in 2.062, now warns. In a future release it will become non-static.
immutable int[] arr = [1, 2];
// ditto
const int[] arr2 = [1, 2];
}
---------
$(P This means that code which accessed such declarations without the $(D this)
reference will no longer compile. Additionally code which depended on the size
of a structure with such fields will have to be fixed:)
---------
struct S
{
immutable int[] arr = [1, 2];
}
void main()
{
auto x = S.arr; // becomes an error in a future release, 'arr' will require the 'this' reference.
// S is size 1 in 2.062 and 2.063. In a future release this will change and the following static assert will pass.
static assert(S.sizeof == size_t.sizeof + size_t.sizeof); // ptr + length for the array
}
---------
$(P To make the field static again, simply use the static keyword. Alternatively
make the field an $(D enum) to turn it into a manifest constant:)
---------
struct S
{
static immutable int[] arr = [1, 2];
enum arr2 = [1, 2];
}
---------
$(P Note however that manifest constants which are arrays are allocated on each usage,
so you may prefer using $(D static) instead.)
$(H3 Rationale:)
$(P Making a field implicitly static based on whether it is const/immutable and has an initializer leads to confusion. The $(D static) keyword can be used to explicitly make any field static.))
$(LI $(LNAME2 ctorqualifier, Constructor qualifiers are taken into account when constructing objects:)
$(P A qualified constructor is now invoked when a $(D const)/$(D immutable)/$(D shared) aggregate object is
instantiated, respectively:)
---------
import std.stdio;
class C
{
this() { writeln("1"); }
this() const { writeln("2"); }
this() immutable { writeln("3"); }
this() shared { writeln("4"); }
}
void main()
{
auto a = new C; // writes "1"
auto b = new const C; // writes "2"
auto c = new immutable C; // writes "3"
auto d = new shared C; // writes "4"
}
---------
$(P This has the consequence that aggregates which have only $(D immutable) or $(D shared)
constructors can no longer be used to instantiate mutable objects:)
---------
class C
{
this() immutable { }
this() shared { }
}
void main()
{
auto c1 = new C; // disallowed
auto c2 = new immutable C; // ok
auto c3 = new shared C; // ok
}
---------
$(P On the other hand, aggregates which do not have $(D shared) or $(D immutable) constructors can
no longer be used to construct $(D shared) or $(D immutable) objects, respectively:)
---------
class C
{
this() { }
}
void main()
{
auto c1 = new C; // ok
auto c2 = new immutable C; // disallowed
auto c3 = new shared C; // disallowed
}
---------
$(P However, if an aggregate has a $(D pure) constructor it can be used to construct an object with any type constructor:)
---------
class C
{
this() pure { }
}
void main()
{
auto c1 = new C; // ok
auto c2 = new immutable C; // ok
auto c3 = new shared C; // ok
}
---------
)
$(LI $(LNAME2 structuralcompare, Struct members which require non-bitwise comparison are now properly compared.)
$(P In earlier releases some struct members such as arrays would be bitwise-compared in a comparison operation.
This has now been changed to be a structural comparison instead:)
---------
struct S
{
char[] data;
}
void main ()
{
auto s1 = S("foo".dup);
auto s2 = S("foo".dup);
assert(s1.data !is s2.data); // both are unique data
assert(s1 == s2); // passes in 2.063
assert(s1.data == s2.data); // equivalent of above
}
---------
$(P If an $(D opEquals) function is not present the compiler rewrites the expression
$(CODE s1 == s2) to $(CODE s1.tupleof == s2.tupleof). Comparing $(D .tupleof) expressions is
also a feature new to D in the 2.063 release.))
$(LI $(LNAME2 slicecopy, Array copy operations now always require using the slice syntax:)
$(P The right-hand-side of an array copy operation now requires using the slice syntax:)
---------
void main()
{
int[][2] x;
int[] y;
int[] z;
x[] = z; // copies z (pointer + length) 2 times to x
y[] = z; // copies each element of z into y (compiler emits warning)
}
---------
$(P If the user intended to write such code they must use the slice syntax for both the source and target arrays:)
---------
void main()
{
int[][2] x;
int[] y;
int[] z;
y[] = z[]; // copies each element of z into y (no warnings)
}
---------
$(H3 Rationale:)
$(P The compiler will emit a warning to make the user aware that the copy operation is arbitrarily expensive.))
$(LI $(LNAME2 passtype, Types no longer act as arguments in $(D typeof) expressions:)
$(P A type can no longer be passed to a function as a value of that type:)
---------
T[] foo(T)(T t)
{
return null;
}
void main()
{
alias int Int;
// used to work (only with an alias), now a compiler error
alias typeof(foo(Int)) IntArray;
}
---------
$(P If the user wants to pass an argument of a certain type, they can use the $(D .init) property:)
---------
T[] foo(T)(T t)
{
return null;
}
void main()
{
alias typeof(foo(int.init)) IntArray; // ok
}
---------
$(H3 Rationale:)
$(P Treating types as expressions in special contexts only leads to confusion.
Instead, the $(D .init) property can be used for such purposes.))
$(LI $(LNAME2 foreachref, The index variable in a foreach range is no longer implicitly a reference:)
$(P The index variable in a foreach range is now by default a value type:)
---------
void main()
{
size_t count;
foreach (n; 0 .. 10)
{
++n;
++count;
}
assert(count == 10); // passes
}
---------
$(P If the user wants to modify the index variable he must use the $(D ref) keyword:)
---------
void main()
{
size_t count;
foreach (ref n; 0 .. 10)
{
++n;
++count;
}
assert(count == 5);
}
---------
$(H3 Rationale:)
$(P Making the index variable implicitly $(D ref) can introduce bugs that are hard to track down.))
$(LI $(LNAME2 hashentry, Associative array entries are no longer default-initialized before assignment:)
$(P An associative array entry used to be default-initialized before assignment took place:)
---------
void main()
{
int[int] aa;
aa[1] = aa[1] + 1; // no Error thrown in 2.062
assert(aa[1] == 1); // worked in 2.062
}
---------
$(P In 2.063, accessing an entry which does not exist will now throw a RangeError:)
---------
void main()
{
int[int] aa;
aa[1] = aa[1] + 1; // RangeError thrown in 2.063
}
---------
$(H3 Rationale:)
$(P Default-initialization during assignment can be a source of bugs.))
$(LI $(LNAME2 constinherit, The const attribute is no longer inherited in overriden methods.)
$(P Method overrides no longer inherit constness of the base method:)
---------
class A
{
void foo() const { }
}
class B : A
{
// used to work in 2.062, now an error
override void foo() { } // note missing 'const'
}
---------
$(P If the user wants to override a const method he has to mark the overriden
method as const:)
---------
class A
{
void foo() const { }
}
class B : A
{
override void foo() const { } // ok
}
---------
$(P The feature allows introducing new overloads based on the constness of the method:)
---------
class A
{
void foo() const { }
}
class B : A
{
// introduces new overload (not override!)
void foo() { }
// if the above overload is introduced the user must either:
// a: re-introduce the const overload to prevent function hijacking
alias super.foo foo; // without this you will get a compiler error
// or b: provide a properly typed override:
override void foo() const { }
}
---------
)
$(LI $(LNAME2 typeofnullconv, $(D typeof(null)) no longer implicitly converts to T[]:)
$(P The following code used to be allowed:)
---------
void f(int[] function() del)
{
assert(!del()); // fails
}
typeof(null) g() { return null; }
void main()
{
f(&g);
f(() => null);
}
---------
$(P However the implicit conversion would end up generating wrong code.
To work around this, make sure the return type is typed properly,
or use $(CODE (T[]).init) in the return expression of a lambda expression:)
---------
void f(int[] function() del)
{
assert(!del()); // passes
}
int[] g() { return null; } // fixed return type
void main()
{
f(&g); // ok
f(() => (int[]).init); // ok
}
---------
)
$(LI $(LNAME2 templatethisattrib, The Template This Parameter now changes the member function qualifier:)
$(P The $(LINK2 template.html#TemplateThisParameter, Template This Parameter) can now be used to infer
the qualifier of $(D this) to member functions:)
---------
struct S
{
void foo(this T)()
{
}
}
void main()
{
immutable S s;
s.foo(); // makes S.foo immutable
}
---------
)
$(LI $(LNAME2 sliceref, Array slices are now r-values:)
$(P Array slices are no longer l-values. This means an address can no longer be
taken of a slice, and slices cannot be passed by ref to functions:)
---------
void foo(ref int[] arr) { arr = new int[10]; }
void main()
{
int[] arr;
foo(arr); // ok
assert(arr.length == 10);
foo(arr[]); // disallowed in 2.063, the slice is an r-value
auto ptr = &arr[1..2]; // disallowed in 2.063, cannot take address of r-value
}
---------
$(P To work around this you can make your function take an r-value if it doesn't
need to reassign and resize the slice, but only needs to read or modify its contents.
Otherwise, to accept both l-values and r-values you can make your function take
its argument by $(D auto ref):)
---------
void take(int[] arr) { }
void takeRef(ref int[] arr) { }
void takeAutoRef(T)(auto ref T[] arr) { }
void main()
{
int[] arr = [1, 2, 3, 4];
take(arr); // ok
takeRef(arr); // ok
takeAutoRef(arr); // ok
int[] arr2 = arr[1 .. 2];
take(arr2); // ok, arr2 is a variable
takeRef(arr2); // ditto
takeAutoRef(arr2); // ditto
take(arr[1 .. 2]); // ok
takeRef(arr[1 .. 2]); // error, cannot pass r-value by reference
takeAutoRef(arr[1 .. 2]); // ok
}
---------
$(H3 Rationale:)
$(P Passing slices by reference had no observable effect when reassigning or
resizing such a slice at the call site, therefore such slices should by default be r-values.
For example, the following code used to be allowed but is now a compile-time error:)
---------
void reAssign(ref int[] arr) { arr = new int[2]; }
void reSize(ref int[] arr) { arr.length = 10; }
void main()
{
int[] arr = [1, 2, 3, 4];
reAssign(arr[0 .. 4]); // reassigning has no observable effect at the call site
assert(arr == [1, 2, 3, 4]);
reSize(arr[0 .. 4]); // resizing has no observable effect at the call site
assert(arr.length == 4);
}
---------
)
$(LI $(LNAME2 thisrefaccess, Accessing a non-static field without a $(D this) reference is only allowed in certain contexts:)
$(P Accessing non-static fields used to be allowed in many contexts, but is now limited to only a few:)
$(P - $(D offsetof), $(D init), and other built-in properties are allowed:)
---------
struct S { int field; }
void main()
{
auto a = S.field.offsetof; // ok, statically known
auto c = S.field.max; // ditto
auto d = S.field; // disallowed, no `this` reference
}
---------
$(P - When invoking static methods of a non-static field:)
---------
struct Foo
{
static struct Bar
{
static int get() { return 0; }
}
Bar bar;
}
void main()
{
static assert(Foo.bar.get() == 0); // ok, equivalent to `typeof(Foo.bar).get()'
}
---------
$(P - When accessing static fields implicitly using an $(LINK2 class.html#AliasThis, $(D alias this)) expression:)
---------
struct Foo
{
static struct Bar
{
static int get() { return 0; }
}
Bar bar;
alias bar this;
}
void main()
{
static assert(Foo.get() == 0); // ok, equivalent to 'typeof(Foo.bar).get()'
}
---------
)
$(LI $(LNAME2 implicitarrayptr, Arrays no longer implicitly convert to a pointer:)
$(P The implicit conversion of an array to a pointer was a deprecated feature:)
---------
void foo(int* p) { }
void main()
{
int[] arr = [1, 2];
foo(arr); // ok if -d switch is used during compilation
}
---------
$(P This feature has now been completely removed. The workaround is to either
use the $(D .ptr) property, or explicitly pass the pointer to the first element:)
---------
void foo(int* p) { }
void main()
{
int[] arr = [1, 2];
foo(arr); // compile error
foo(arr.ptr); // ok
foo(&arr[0]); // ok
}
---------
))
$(BUGSTITLE Language Enhancements,
$(LI $(LNAME2 uniqueinference, Expressions which return unique objects can be implicitly casted to immutable:)
$(P Expressions such as $(D new) for objects and arrays, and $(D dup) for arrays, can now be inferred to be unique.
This allows the compiler to implicitly convert such an expression to immutable:)
---------
class C { }
void main()
{
immutable int[] arr1 = new int[](3); // ok
immutable int[] arr2 = [1, 2, 3].dup; // ok in 2.063
immutable C[] arr3 = [new C, new C].dup; // ok in 2.063
}
---------
)
$(LI $(LNAME2 staticarrayvoid, Static array of void can now be user-initialized.)
$(P A static array of void could not be initialized in user-code:)
---------
void main()
{
void[2] varr1; // error in 2.062
void[2] varr2 = (void[2]).init; // error in 2.062
void[2] varr3 = void; // ok in 2.062
}
---------
$(P In 2.063, an explicit initializer can be used:)
---------
void main()
{
void[2] varr1; // still an error in 2.063
void[2] varr2 = (void[2]).init; // ok in 2.063
void[2] varr3 = void; // ok in 2.063
}
---------
$(P The $(D .init) property effectively zero-initializes the array.)
$(H3 Rationale:)
$(P The restriction has been lifted to allow generic code to use $(D .init) without
having to specialize for static void arrays.))
$(LI $(LNAME2 multiinvariant, Aggregates can now contain multiple invariants:)
$(P If an aggregate type has multiple invariants, the invariants' bodies will be merged
into a single invariant function and will be run in sequence. Note that the code in
one invariant cannot reference code or data in another invariant:)
---------
struct S
{
int x;
void foo() { }
invariant()
{
int local;
assert(x != 0);
}
invariant()
{
// local = 1; // invariant does not have access to the other invariant's body
assert(x % 2 == 0);
}
}
void main()
{
S s = S(2);
s.foo(); // invoking public function triggers both invariants in sequence
}
---------
)
$(LI $(LNAME2 attribinference, Methods of templated aggregates can now infer attributes:)
$(P If a function with some attributes instantiates a templated aggregate,
it's member functions will infer those attributes:)
---------
struct S(T)
{
T square(T x)
{
return x * x;
}
}
void main() pure
{
S!int s; // S!int.square becomes pure and callable from main()
assert(s.square(2) == 4); // ok
}
---------
)
$(LI $(LNAME2 isexpident, $(B is expression) no longer requires an identifier:)
$(P In some cases the $(LINK2 expression.html#IsExpression, is expression) required an
identifier even when you didn't have a use for it:)
---------
void main()
{
alias AA = string[int];
static if (is(AA _ == V[K], V, K))
{
pragma(msg, _); // prints string[int]
pragma(msg, K); // prints int
pragma(msg, V); // prints string
}
}
---------
$(P The identifier is no longer required, so the above can be rewritten to:)
---------
void main()
{
alias AA = string[int];
static if (is(AA == V[K], V, K))
{
pragma(msg, AA); // prints string[int]
pragma(msg, K); // prints int
pragma(msg, V); // prints string
}
}
---------
)
$(LI $(LNAME2 implicitarraycast, Dynamic arrays of known size can be implicitly cast to static arrays in some contexts:)
$(P In some contexts the compiler knows the size of a dynamic array or of a slice of an array.
In such a case the compiler will allow an implicit conversion to a static array of the same size:)
---------
void foo(int[4] x) { }
void main()
{
int[] arr = [1, 2, 3, 4, 5, 6, 7, 8];
foo(arr[0 .. 4]); // ok
}
---------
$(P Another example, where a string is converted to a reference to a static array:)
---------
string str = "aaaabbbbccccdddd";
void foo(ref const(char)[16] buf)
{
assert(buf.ptr is str.ptr);
}
void main()
{
foo(str[0..16]); // ok
}
---------
$(H3 Limitations:)
$(P - This feature does not yet work with complex expressions where it might be
reasonable to assume the size of a slice:)
---------
void foo(int[4] x) { }
void main()
{
int[] arr = [1, 2, 3, 4, 5, 6, 7, 8];
foreach (i; 0 .. 4)
{
foo(arr[i .. i + 4]); // not yet supported
}
}
---------
)
$(LI $(LNAME2 tupleinitvoid, Tuples can now be void-initialized:)
$(P You can now void-initialize a tuple variable:)
---------
template Tuple(T...)
{
alias T Tuple;
}
void main()
{
Tuple!(int, int) tup1 = void; // ok
}
---------
$(P Upon such initialization the values in the tuple are undetermined.))
$(LI $(LNAME2 templconstraint, Template constraints can now be put after the inheritance list:)
$(P Template constraints used to be allowed only before the inheritance list, leading to
code where the inheritance list could be hard to spot:)
---------
class Foo(T1, T2)
if (is(T1 == int) && is(T2 == string)) : Base
{
}
---------
$(P This restriction has been lifted, so you can now write:)
---------
class Foo(T1, T2) : Base
if (is(T1 == int) && is(T2 == string))
{
}
---------
)
$(LI $(LNAME2 tupleequality, Tuples can now be compared for equality:)
$(P Example:)
---------
struct Tuple(T...) { T field; alias field this; }
void main()
{
auto tup1 = Tuple!(int, int)(1, 2);
auto tup2 = Tuple!(int, int)(1, 2);
auto tup3 = Tuple!(int, int)(1, 3);
assert(tup1 == tup2); // works since 2.063
assert(tup1 != tup3); // works since 2.063
}
---------
$(P This also means you can now compare $(B ParameterStorageClassTuple) instances from std.traits:)
---------
import std.traits;
void func1(ref int x, ref int y) { }
void func2(ref float x, ref float y) { }
void main()
{
alias Storages = ParameterStorageClassTuple;
assert(Storages!func1 == Storages!func2);
}
---------
$(P In addition to that, builtin $(D .tupleof) expressions can be used to easily compare fields of an aggregate:)
---------
struct S
{
char[] a, b;
// Implements equality test against another instance of this type.
bool opEquals(S rhs) { return this.tupleof == rhs.tupleof; }
}
void main()
{
S s1 = S("a".dup, "b".dup);
S s2 = S("a".dup, "b".dup);
assert(s1 == s2);
}
---------
$(P This also allows you to implement a structural equality test against an instance of a different
type:)
---------
struct S1
{
char[] a, b;
// Implements a structural equality test against any other type T
bool opEquals(T)(T rhs) { return this.tupleof == rhs.tupleof; }
}
struct S2
{
string x, y;
}
void main()
{
auto s1 = S1("123".dup, "456".dup);
auto s2 = S2("123", "456");
assert(s1 == s2);
}
---------
$(P Since tuples can be sliced you can use this feature to compare a subset of tuples:)
---------
struct S
{
int a, b, c, d, e;
bool opEquals(S rhs)
{
// compares a, b, d, and e
return this.tupleof[0..2] == rhs.tupleof[0..2] &&
this.tupleof[3..5] == rhs.tupleof[3..5];
}
}
void main()
{
S s1 = S(1, 2, 0, 3, 4);
S s2 = S(1, 2, 1, 3, 4);
assert(s1 == s2);
}
---------
)
$(LI $(LNAME2 initfield, Fields with initializers can now be re-initialized in a const constructor:)
$(P You can now initialize a field in a const constructor even if
such a field already has an initializer:)
---------
struct S
{
bool field = true;
this(int v) const
{
field = false; // ok
}
}
---------
)
$(LI $(LNAME2 isnestedtrait, Added the $(B isNested) trait for discovery of aggregates and functions with context pointers:)
$(P The new $(XLINK2 traits.html#isNested, isNested) trait allows you to discover whether an aggregate or function
contains a context pointer:)
---------
void main()
{
int x;
struct S1 { void f() { x++; } }
static struct S2 { }
void f1() { x++; }
static void f2() { }
static assert(__traits(isNested, S1));
static assert(__traits(isNested, f1));
static assert(!__traits(isNested, S2));
static assert(!__traits(isNested, f2));
}
---------
)
$(LI $(LNAME2 nestedtemplate, Templates can now be nested inside of functions:))