Array operations

Nick Treleaven nick at geany.org
Sat May 3 11:18:00 UTC 2025


On Friday, 2 May 2025 at 22:19:53 UTC, Andy Valencia wrote:
> In the following code, two questions.  First, is there any 
> difference between "x[] = y" and "x[] = y[]"?  It appears not.

`y` is already a slice `int[]`, so slicing it does not change the 
type. Slicing without indices selects all elements, it does not 
change the elements the expression refers to.

> Second, in assigning from arrays of differing sizes, Phobos 
> causes an illegal instruction, rather than the sort of 
> exception I'd have expected.  I'm curious why they stepped away 
> from D's exception architecture?

It throws a RangeError rather than an Exception. One advantage is 
that allows indexing an array to be used in `nothrow` code. Also, 
bounds checks can be turned off, in which case nothing would be 
thrown, and code attempting to catch this occurence would never 
be called.

Indexing past the end of a slice is a programming error which is 
not supposed to be recoverable from. If the slice length mismatch 
is caused by a runtime value, that's still a programmer error 
because the runtime value was not checked for validity.


More information about the Digitalmars-d-learn mailing list