Unexpected range assignment behaviour

H. S. Teoh hsteoh at qfbox.info
Fri Jul 19 14:44:55 UTC 2024


On Fri, Jul 19, 2024 at 09:34:13AM +0000, Lewis via Digitalmars-d-learn wrote:
> ```
> string[3][string] lookup;
> string[] dynArray = ["d", "e", "f"];
> lookup["test"] = dynArray[0..$];
> ```
> 
> This fails at runtime with RangeError. But if I change that last line to:
> 
> ```
> lookup["test"] = dynArray[0..3];
> ```
> 
> then it works. But the value of $ here is 3. Why do I get a RangeError
> at runtime even though the slice is the correct size (and the same
> size as the hardcoded one that works)? I would have expected to only
> get a RangeError if at runtime the value turned out to be wrong.

Sounds like a bug.  First, there's an incompatibility between AA value
type and the type being assigned: the value type of `lookup` is a static
array, whereas a slice is a dynamic array.  But since the compiler
allows this assignment, it should work.  I suspect there's a frontend
bug somewhere in how assignments of slices to static arrays are
implemented.


T

-- 
MAS = Mana Ada Sistem?


More information about the Digitalmars-d-learn mailing list