You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Introduce .length property before use.
Tweak array length example.
Link .ptr property to assignment section.
Make properties example compile & extend it.
Copy file name to clipboardExpand all lines: spec/arrays.dd
+27-13Lines changed: 27 additions & 13 deletions
Original file line number
Diff line number
Diff line change
@@ -295,23 +295,28 @@ writeln(b[7]); // 10
295
295
296
296
$(H2 $(LNAME2 array-length, Array Length))
297
297
298
+
$(P The $(RELATIVE_LINK2 array-properties, `.length` property)
299
+
for static and dynamic arrays gives the number of elements in the array.)
300
+
298
301
$(P When indexing or slicing a static or dynamic array,
299
302
the symbol $(D $) represents the length of the array.
300
303
)
301
304
302
305
$(SPEC_RUNNABLE_EXAMPLE_RUN
303
306
---------
304
307
int[4] foo;
305
-
int[] bar = foo;
308
+
int[] bar;
306
309
307
-
// These expressions are equivalent:
310
+
// These assignments are equivalent:
308
311
bar = foo;
309
312
bar = foo[];
310
313
bar = foo[0 .. 4];
311
314
bar = foo[0 .. $];
312
315
bar = foo[0 .. foo.length];
316
+
assert(bar.length == 4);
313
317
314
-
int* p = foo.ptr;
318
+
int* p = foo.ptr; // a pointer has no length property
319
+
bar = p[0 .. 4]; // OK
315
320
//bar = p[0 .. $]; // error, '$' is not defined, since p is not an array
316
321
317
322
int i;
@@ -654,28 +659,37 @@ Returns an array literal with each element of the literal being the $(D .init) p
654
659
array. It is of type $(D size_t).)
655
660
$(TROW $(D .capacity), Returns the length an array can grow to without reallocating.
656
661
See $(RELATIVE_LINK2 capacity-reserve, here) for details.)
657
-
$(TROW $(D .ptr), Returns a pointer to the first element of the array.)
662
+
$(TROW $(D .ptr), Returns a pointer to the first element of the array.
663
+
See $(RELATIVE_LINK2 assignment, assignment).)
658
664
$(TROW $(D .dup), Create a dynamic array of the same size and copy the contents of the array into it. The copy will have any immutability or const stripped. If this conversion is invalid the call will not compile.)
659
665
$(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. If this conversion is invalid the call will not compile.)
660
666
)
661
667
662
668
$(P Examples:)
663
669
664
-
$(SPEC_RUNNABLE_EXAMPLE_FAIL
670
+
$(SPEC_RUNNABLE_EXAMPLE_RUN
665
671
---------
666
672
int* p;
667
673
int[3] s;
668
674
int[] a;
669
675
670
-
p.length; // error, length not known for pointer
671
-
s.length; // compile time constant 3
672
-
a.length; // runtime value
676
+
size_t len;
677
+
//len = p.length; // error, pointer has no length property
678
+
enum sl = s.length; // compile time constant
679
+
static assert(sl == 3);
680
+
681
+
len = a.length; // runtime value
682
+
assert(len == 0);
683
+
684
+
//a = p.dup; // error, length not known
685
+
a = s.dup; // allocates an array of 3 elements, copies
686
+
// elements of `s` into it
687
+
assert(a == s[]);
688
+
assert(a.ptr != s.ptr);
673
689
674
-
p.dup; // error, length not known
675
-
s.dup; // creates an array of 3 elements, copies
676
-
// elements of s into it
677
-
a.dup; // creates an array of a.length elements, copies
678
-
// elements of a into it
690
+
int[] b = a.dup; // allocates a new array of `a.length` elements, copies
0 commit comments