void[] vs ubyte[] - differences?
Nick Treleaven
nick at geany.org
Thu Jul 17 16:26:43 UTC 2025
On Wednesday, 16 July 2025 at 13:29:01 UTC, z wrote:
> I also see this in the language documentation :
> ```
> A void array cannot be indexed.
> ```
> But i can slice it just fine in DMD 2.111...
>
> Is this by design or is there a hole in the language
> specification?
The sentence before says:
> Array indices in slicing operations are interpreted as byte
> indices
And the example shows slicing:
```d
void[] arr = data1; // OK, int[] implicit converts
to void[].
...
arr[0..4] = [5]; // Assign first 4 bytes to 1
int element
```
Slicing a void array is useful as it can refer to multiple bytes.
Indexing a void array is not really useful because if the array
is only holding bytes, then why not use byte[] instead? If it's
not a byte, then indexing it is probably wrong anyway (either by
design, or because dealing with a single byte at a time is
inefficient).
> By extension what the differences between `void`, `ubyte`,
> `void[]`, `ubyte[]` really? The language documentation doesn't
> appear to be explaining everything.
> Thanks in advance
The docs explain the difference between the two array types.
Perhaps there should be an entry for `void` in the types page. A
`void` value cannot be accessed. `void.sizeof` is 1 so that a
void array can have length equivalent to the number of bytes in
the array.
More information about the Digitalmars-d-learn
mailing list