Skip to content

Commit cf49126

Browse files
authored
[spec] Improve array properties docs (#3905)
Introduce .length property before use. Tweak array length example. Link .ptr property to assignment section. Make properties example compile & extend it.
1 parent 7e28e8e commit cf49126

File tree

1 file changed

+27
-13
lines changed

1 file changed

+27
-13
lines changed

spec/arrays.dd

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -295,23 +295,28 @@ writeln(b[7]); // 10
295295

296296
$(H2 $(LNAME2 array-length, Array Length))
297297

298+
$(P The $(RELATIVE_LINK2 array-properties, `.length` property)
299+
for static and dynamic arrays gives the number of elements in the array.)
300+
298301
$(P When indexing or slicing a static or dynamic array,
299302
the symbol $(D $) represents the length of the array.
300303
)
301304

302305
$(SPEC_RUNNABLE_EXAMPLE_RUN
303306
---------
304307
int[4] foo;
305-
int[] bar = foo;
308+
int[] bar;
306309

307-
// These expressions are equivalent:
310+
// These assignments are equivalent:
308311
bar = foo;
309312
bar = foo[];
310313
bar = foo[0 .. 4];
311314
bar = foo[0 .. $];
312315
bar = foo[0 .. foo.length];
316+
assert(bar.length == 4);
313317

314-
int* p = foo.ptr;
318+
int* p = foo.ptr; // a pointer has no length property
319+
bar = p[0 .. 4]; // OK
315320
//bar = p[0 .. $]; // error, '$' is not defined, since p is not an array
316321

317322
int i;
@@ -654,28 +659,37 @@ Returns an array literal with each element of the literal being the $(D .init) p
654659
array. It is of type $(D size_t).)
655660
$(TROW $(D .capacity), Returns the length an array can grow to without reallocating.
656661
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).)
658664
$(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.)
659665
$(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.)
660666
)
661667

662668
$(P Examples:)
663669

664-
$(SPEC_RUNNABLE_EXAMPLE_FAIL
670+
$(SPEC_RUNNABLE_EXAMPLE_RUN
665671
---------
666672
int* p;
667673
int[3] s;
668674
int[] a;
669675

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);
673689

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
691+
// elements of `a` into it
692+
assert(b !is a); // b is an independent copy of a
679693
---------
680694
)
681695

0 commit comments

Comments
 (0)