Skip to content

Commit e8e6d30

Browse files
ntreldlang-bot
authored andcommitted
[spec/arrays] Improve void array docs
Indexing a void array is illegal. Mention tail qualified void arrays. Link to array cast. Extend & split example. Add links to void arrays.
1 parent b2d16a9 commit e8e6d30

File tree

3 files changed

+26
-12
lines changed

3 files changed

+26
-12
lines changed

spec/arrays.dd

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1281,43 +1281,55 @@ writefln("the string is '%s'", str);
12811281

12821282
$(H3 $(LNAME2 void_arrays, Void Arrays))
12831283

1284-
$(P There is a special type of array which acts as a wildcard that can hold
1285-
arrays of any kind, declared as $(D void[]). Void arrays are used for
1284+
$(P There are special types of array with `void` element type which can hold
1285+
arrays of any kind. Void arrays are used for
12861286
low-level operations where some kind of array data is being handled, but
12871287
the exact type of the array elements are unimportant. The $(D .length) of a
12881288
void array is the length of the data in bytes, rather than the number of
1289-
elements in its original type. Array indices in indexing and slicing
1289+
elements in its original type. Array indices in slicing
12901290
operations are interpreted as byte indices.)
12911291

1292-
$(P Arrays of any type can be implicitly converted to a void array; the
1292+
$(P Arrays of any type can be implicitly converted to a (tail qualified) void array - the
12931293
compiler inserts the appropriate calculations so that the $(D .length) of
12941294
the resulting array's size is in bytes rather than number of elements. Void
1295-
arrays cannot be converted back to the original type without using a cast,
1295+
arrays cannot be converted back to the original type without using an
1296+
$(DDSUBLINK spec/expression, cast_array, array cast),
12961297
and it is an error to convert to an array type whose element size does not
12971298
evenly divide the length of the void array.)
12981299

1299-
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
1300+
$(SPEC_RUNNABLE_EXAMPLE_RUN
13001301
---------
13011302
void main()
13021303
{
13031304
int[] data1 = [1,2,3];
1304-
long[] data2;
13051305

13061306
void[] arr = data1; // OK, int[] implicit converts to void[].
13071307
assert(data1.length == 3);
13081308
assert(arr.length == 12); // length is implicitly converted to bytes.
13091309

1310-
//data1 = arr; // Illegal: void[] does not implicitly
1310+
arr[0..4] = [5]; // Assign first 4 bytes to 1 int element
1311+
assert(data1 == [5,2,3]);
1312+
1313+
//data1 = arr; // Error: void[] does not implicitly
13111314
// convert to int[].
1312-
int[] data3 = cast(int[]) arr; // OK, can convert with explicit cast.
1313-
data2 = cast(long[]) arr; // Runtime error: long.sizeof == 8, which
1315+
int[] data2 = cast(int[]) arr; // OK, can convert with explicit cast.
1316+
assert(data2 is data1);
1317+
}
1318+
---------
1319+
)
1320+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
1321+
---------
1322+
void main()
1323+
{
1324+
void[] arr = new void[12];
1325+
long[] bad = cast(long[]) arr; // Runtime error: long.sizeof == 8, which
13141326
// does not divide arr.length, which is 12
13151327
// bytes.
13161328
}
13171329
---------
13181330
)
13191331

1320-
$(P Void arrays can also be static if their length is known at
1332+
$(P Void arrays can be static arrays if their length is known at
13211333
compile-time. The length is specified in bytes:)
13221334

13231335
$(SPEC_RUNNABLE_EXAMPLE_COMPILE

spec/garbage.dd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,8 @@ $(H2 $(LNAME2 precise_dataseg, Precise Scanning of the DATA and TLS segment))
520520
---------
521521
This doesn't work for TLS memory, though.
522522

523+
See also: $(DDSUBLINK spec/arrays, void_arrays, void arrays).
524+
523525
$(H2 $(LNAME2 gc_parallel, Parallel marking))
524526

525527
$(P By default the garbage collector uses all available CPU cores to mark the heap.)

spec/type.dd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ $(H3 $(LEGACY_LNAME2 Implicit Conversions, implicit-conversions, Implicit Conver
232232

233233
$(UL
234234
$(LI All types implicitly convert to $(RELATIVE_LINK2 noreturn, `noreturn`).)
235-
$(LI Static and dynamic arrays implicitly convert to $(DDSUBLINK spec/arrays, void_arrays, `void[]`).)
235+
$(LI Static and dynamic arrays implicitly convert to $(DDSUBLINK spec/arrays, void_arrays, `void` arrays).)
236236
$(LI $(DDSUBLINK spec/function, function-pointers-delegates, Function pointers and delegates)
237237
can convert to covariant types.)
238238
)

0 commit comments

Comments
 (0)