foreach counter now must be size_t ?
H. S. Teoh
hsteoh at quickfur.ath.cx
Mon Feb 4 22:45:02 UTC 2019
On Mon, Feb 04, 2019 at 10:26:08PM +0000, Johan Engelen via Digitalmars-d wrote:
> This code now gives a deprecation message (>= 2.084):
> ```
> void foo(int[] arr) {
> foreach (uint i, ref elem; arr) { }
> }
> ```
> Deprecation: foreach: loop index implicitly converted from `size_t` to
> `uint`
>
> This is in contrast to the spec that says that "The index must be of
> int, uint, long or ulong type, it cannot be ref, and it is set to be
> the index of the array element."
> https://dlang.org/spec/statement.html#foreach_over_arrays
>
> Did someone forget to update the spec? Or is it a frontend bug?
[...]
I'd expect the loop index should be of a type that can accomodate the
largest possible array index. On 64-bit systems, that would be ulong,
which size_t is aliased to when compiling for 64-bit. On 32-bit, size_t
would be uint, so the above code should be compilable with -m32.
T
--
He who does not appreciate the beauty of language is not worthy to bemoan its flaws.
More information about the Digitalmars-d
mailing list