@@ -49,18 +49,12 @@ int[3] s;
49
49
a template expansion.
50
50
)
51
51
52
- $(V1
53
- $(P Static arrays are value types, but as in C
54
- static arrays are passed to functions by reference
55
- and cannot be returned from functions.
56
- )
57
- )
58
- $(V2
52
+
59
53
$(P Static arrays are value types. Unlike in C and D version 1,
60
54
static arrays are passed to functions by value.
61
55
Static arrays can also be returned by functions.
62
56
)
63
- )
57
+
64
58
65
59
$(H4 $(LNAME2 dynamic-arrays, Dynamic Arrays))
66
60
@@ -418,19 +412,12 @@ double matrix[3,3];
418
412
419
413
$(H3 $(LNAME2 array-length, Array Length))
420
414
421
- $(V1
422
- $(P Within the [ ] of a static or a dynamic array,
423
- the variable $(D length)
424
- is implicitly declared and set to the length of the array.
425
- The symbol $(D $) can also be so used.
426
- )
427
- )
428
- $(V2
415
+
429
416
$(P Within the [ ] of a static or a dynamic array,
430
417
the symbol $(D $)
431
418
represents the length of the array.
432
419
)
433
- )
420
+
434
421
435
422
---------
436
423
int[4] foo;
@@ -440,21 +427,14 @@ int* p = &foo[0];
440
427
// These expressions are equivalent:
441
428
bar[]
442
429
bar[0 .. 4]
443
- $(V1 bar[0 .. length]
444
- )bar[0 .. $]
430
+ bar[0 .. $]
445
431
bar[0 .. bar.length]
446
432
447
- $(V1 p[0 .. length] // 'length' is not defined, since p is not an array
448
- bar[0]+length // 'length' is not defined, out of scope of [ ]
449
433
450
- bar[length-1] // retrieves last element of the array
451
- )
452
- $(V2
453
- p[0 .. $] // '$' is not defined, since p is not an array
454
- bar[0]+$ // '$' is not defined, out of scope of [ ]
434
+ p[0 .. $] // '$' is not defined, since p is not an array
435
+ bar[0]+$ // '$' is not defined, out of scope of [ ]
455
436
456
- bar[$-1] // retrieves last element of the array
457
- )
437
+ bar[$-1] // retrieves last element of the array
458
438
---------
459
439
460
440
$(H3 $(LNAME2 array-properties, Array Properties))
@@ -464,20 +444,19 @@ $(H3 $(LNAME2 array-properties, Array Properties))
464
444
$(TABLE_2COLS Static Array Properties,
465
445
$(THEAD Property, Description)
466
446
$(TROW $(D .init),
467
- $(V1 returns the default initializer for the element type.)
468
- $(V2 Returns an array literal with each element of the literal being the $(D .init) property of the array element type.))
447
+ Returns an array literal with each element of the literal being the $(D .init) property of the array element type.)
469
448
$(TROW $(D .sizeof), Returns the array length multiplied by
470
449
the number of bytes per array element.)
471
450
$(TROW $(D .length), Returns the number of elements in the array.
472
451
This is a fixed quantity for static arrays. It is of type $(D size_t).)
473
452
$(TROW $(D .ptr), Returns a pointer to the first element of the array.)
474
453
$(TROW $(D .dup), Create a dynamic array of the same size and copy the contents of the array into it.)
475
- $(TROW $(D .idup), Create a dynamic array of the same size and copy the contents of the array into it.
476
- The copy is typed as being immutable. $(I D 2.0 only))
454
+ $(TROW $(D .idup), Create a dynamic array of the same size and copy the contents of the array into it. The copy is typed as being immutable.)
477
455
$(TROW $(D .reverse), Reverses in place the order of the elements in the array.
478
456
Returns the array.)
479
457
$(TROW $(D .sort), Sorts in place the order of the elements in
480
- the array. Returns the array.))
458
+ the array. Returns the array.)
459
+ )
481
460
482
461
$(P Dynamic array properties are:)
483
462
@@ -508,13 +487,9 @@ $(V2 Returns an array literal with each element of the literal being the $(D .in
508
487
$(P For the $(D .sort) property to work on arrays of
509
488
structs or unions, the struct or union definition must
510
489
define the function:
511
- $(V1
512
- $(D int opCmp(S)) or
513
- $(D int opCmp(S*)).
514
- )
515
- $(V2
490
+
516
491
$(D int opCmp(ref const S) const).
517
- )
492
+
518
493
The type $(D S) is the type of the struct or union.
519
494
This function will determine the sort ordering.
520
495
)
@@ -562,40 +537,14 @@ array = array[0..7];
562
537
563
538
$(P To maximize efficiency, the runtime always tries to resize the
564
539
array in place to avoid extra copying.
565
- $(V1 It will always do a copy if the new size is larger and the array
566
- was not allocated via the new operator or a previous resize operation.)
567
- $(V2 It will always do a copy if the new size is larger and the array
540
+
541
+ It will always do a copy if the new size is larger and the array
568
542
was not allocated via the new operator or resizing in place would
569
- overwrite valid data in the array.)
543
+ overwrite valid data in the array.
570
544
)
571
545
572
- $(V1 $(P This means that if there is an array slice immediately following the
573
- array being resized, the resized array could overlap the slice; i.e.:
574
- )
575
546
576
- ---------
577
- char[] a = new char[20];
578
- char[] b = a[0..10];
579
- char[] c = a[10..20];
580
547
581
- b.length = 15; // always resized in place because it is sliced
582
- // from a[] which has enough memory for 15 chars
583
- b[11] = 'x'; // a[11] and c[1] are also affected
584
-
585
- a.length = 1;
586
- a.length = 20; // no net change to memory layout
587
-
588
- c.length = 12; // always does a copy because c[] is not at the
589
- // start of a gc allocation block
590
- c[5] = 'y'; // does not affect contents of a[] or b[]
591
-
592
- a.length = 25; // may or may not do a copy
593
- a[3] = 'z'; // may or may not affect b[3] which still overlaps
594
- // the old a[3]
595
- ---------
596
- )
597
-
598
- $(V2
599
548
For example:
600
549
601
550
---------
@@ -623,12 +572,12 @@ a.length = 25; // This always reallocates because if c extended in place,
623
572
// which will still be true for a.
624
573
a[15] = 'z'; // does not affect c, because either a or c has reallocated.
625
574
---------
626
- )
575
+
627
576
628
577
$(P To guarantee copying behavior, use the .dup property to ensure
629
- a unique array that can be resized. $(V2 Also, one may use the phobos
578
+ a unique array that can be resized. Also, one may use the phobos
630
579
$(D .capacity) property to determine how many elements can be appended
631
- to the array without reallocating.)
580
+ to the array without reallocating.
632
581
)
633
582
634
583
$(P These issues also apply to appending arrays with the ~= operator.
@@ -646,7 +595,7 @@ while (1) {
646
595
c = getinput();
647
596
if (!c)
648
597
break;
649
- $(V2 ++array.length;)$(V1 array.length = array.length + 1;)
598
+ ++array.length;
650
599
array[array.length - 1] = c;
651
600
}
652
601
---------
@@ -663,7 +612,7 @@ for (i = 0; ; i++) {
663
612
if (!c)
664
613
break;
665
614
if (i == array.length)
666
- $(V2 array.length *= 2;)$(V1 array.length = array.length * 2;)
615
+ array.length *= 2;
667
616
array[i] = c;
668
617
}
669
618
array.length = i;
@@ -675,8 +624,8 @@ array.length = i;
675
624
input from the console - it's unlikely to be longer than 80.
676
625
)
677
626
678
- $(V2 $( P Also, you may wish to utilize the phobos $(D reserve)
679
- function to pre-allocate array data to use with the append operator.))
627
+ $(P Also, you may wish to utilize the phobos $(D reserve)
628
+ function to pre-allocate array data to use with the append operator.)
680
629
681
630
$(H4 $(LNAME2 func-as-property, Functions as Array Properties))
682
631
@@ -696,7 +645,7 @@ $(H3 $(LNAME2 bounds, Array Bounds Checking))
696
645
697
646
$(P It is an error to index an array with an index that is less than
698
647
0 or greater than or equal to the array length. If an index is
699
- out of bounds, $(V1 an ArrayBoundsError)$(V2 a RangeError) exception is
648
+ out of bounds, a RangeError exception is
700
649
raised if detected at runtime, and an error if detected at compile
701
650
time. A program may not rely on array bounds checking happening, for
702
651
example, the following program is incorrect:
@@ -796,23 +745,7 @@ $(H4 $(LNAME2 strings, Strings))
796
745
String literals are immutable (read only).
797
746
)
798
747
799
- $(V1
800
- ---------
801
- char[] str;
802
- char[] str1 = "abc";
803
- str[0] = 'b'; // error, "abc" is read only, may crash
804
- ---------
805
-
806
- $(P The name $(CODE string) is aliased to $(CODE char[]),
807
- so the above declarations could be equivalently written as:
808
- )
809
748
810
- ---------
811
- string str;
812
- string str1 = "abc";
813
- ---------
814
- )
815
- $(V2
816
749
---------
817
750
char[] str1 = "abc"; // error, "abc" is not mutable
818
751
char[] str2 = "abc".dup; // ok, make mutable copy
@@ -831,7 +764,7 @@ string str3 = "abc"; // ok
831
764
string str4 = str1; // error, str4 is not mutable
832
765
string str5 = str1.idup; // ok, make immutable copy
833
766
---------
834
- )
767
+
835
768
$(P $(CODE char[]) strings are in UTF-8 format.
836
769
$(CODE wchar[]) strings are in UTF-16 format.
837
770
$(CODE dchar[]) strings are in UTF-32 format.
@@ -882,14 +815,10 @@ str ~= "\0";
882
815
)
883
816
884
817
---------
885
- $(V1
886
- cast(wchar [])"abc" // this is an array of wchar characters
887
- "abc"w // so is this
888
- )
889
- $(V2
818
+
890
819
cast(immutable(wchar) [])"abc" // this is an array of wchar characters
891
820
"abc"w // so is this
892
- )
821
+
893
822
---------
894
823
895
824
$(P String literals that do not have a postfix character and that
@@ -976,8 +905,8 @@ $(H4 $(LNAME2 implicit-conversions, Implicit Conversions))
976
905
977
906
$(UL
978
907
$(LI $(D T[]))
979
- $(V1 $(LI $(D U[])))
980
- $(V2 $( LI $(D const(U)[]) ))
908
+
909
+ $(LI $(D const(U)[]))
981
910
$(LI $(D void[]))
982
911
)
983
912
@@ -986,8 +915,8 @@ $(H4 $(LNAME2 implicit-conversions, Implicit Conversions))
986
915
)
987
916
988
917
$(UL
989
- $(V1 $(LI $(D U[])))
990
- $(V2 $( LI $(D const(U)[]) ))
918
+
919
+ $(LI $(D const(U)[]))
991
920
$(LI $(D void[]))
992
921
)
993
922
0 commit comments