Skip to content

Commit be37442

Browse files
authored
[arrays.dd] Move *Array Length* section straight after *Slicing* (#3170)
* Move *Array Length* section after *Slicing* Remove redundant example in *Slicing*. Make example compile. * missing ;
1 parent 4b10ebd commit be37442

File tree

1 file changed

+28
-50
lines changed

1 file changed

+28
-50
lines changed

spec/arrays.dd

Lines changed: 28 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -219,27 +219,6 @@ foo(b[1]); // equivalent to foo(3)
219219
)
220220

221221
$(P $(I Identifier)[] is shorthand for a slice of the entire array.
222-
For example, the assignments to b:
223-
)
224-
225-
$(SPEC_RUNNABLE_EXAMPLE_RUN
226-
---------
227-
int[10] a = [ 1,2,3,4,5,6,7,8,9,10 ];
228-
int[] b1, b2, b3, b4;
229-
230-
b1 = a;
231-
b2 = a[];
232-
b3 = a[0 .. a.length];
233-
b4 = a[0 .. $];
234-
writeln(b1);
235-
writeln(b2);
236-
writeln(b3);
237-
writeln(b4);
238-
239-
---------
240-
)
241-
242-
$(P are all semantically equivalent.
243222
)
244223

245224
$(P Slicing
@@ -264,6 +243,34 @@ writeln(b[7]); // 10
264243
$(P See also $(GLINK2 expression, SliceExpression).)
265244

266245

246+
$(H2 $(LNAME2 array-length, Array Length))
247+
248+
$(P When indexing or slicing a static or dynamic array,
249+
the symbol $(D $) represents the length of the array.
250+
)
251+
252+
$(SPEC_RUNNABLE_EXAMPLE_RUN
253+
---------
254+
int[4] foo;
255+
int[] bar = foo;
256+
257+
// These expressions are equivalent:
258+
bar = foo;
259+
bar = foo[];
260+
bar = foo[0 .. 4];
261+
bar = foo[0 .. $];
262+
bar = foo[0 .. foo.length];
263+
264+
int* p = foo.ptr;
265+
//bar = p[0 .. $]; // error, '$' is not defined, since p is not an array
266+
267+
int i;
268+
//i = foo[0]+$; // error, '$' is not defined, out of scope of [ ]
269+
i = bar[$-1]; // retrieves last element of the array
270+
---------
271+
)
272+
273+
267274
$(H2 $(LNAME2 array-copying, Array Copying))
268275

269276
$(P When the slice operator appears as the left-hand side of an
@@ -513,35 +520,6 @@ static assert(!__traits(compiles, matrix[5][2])); // Array index out of bounds.
513520
---------
514521
)
515522

516-
$(H2 $(LNAME2 array-length, Array Length))
517-
518-
519-
$(P Within the [ ] of a static or a dynamic array,
520-
the symbol $(D $)
521-
represents the length of the array.
522-
)
523-
524-
525-
$(SPEC_RUNNABLE_EXAMPLE_FAIL
526-
---------
527-
int[4] foo;
528-
int[] bar = foo;
529-
int* p = &foo[0];
530-
531-
// These expressions are equivalent:
532-
bar[]
533-
bar[0 .. 4]
534-
bar[0 .. $]
535-
bar[0 .. bar.length]
536-
537-
538-
p[0 .. $] // '$' is not defined, since p is not an array
539-
bar[0]+$ // '$' is not defined, out of scope of [ ]
540-
541-
bar[$-1] // retrieves last element of the array
542-
---------
543-
)
544-
545523
$(H2 $(LNAME2 array-properties, Array Properties))
546524

547525
$(P Static array properties are:)

0 commit comments

Comments
 (0)