Jump to page: 1 25  
Page
Thread overview
foreach counter now must be size_t ?
Feb 04, 2019
Johan Engelen
Feb 04, 2019
H. S. Teoh
Feb 04, 2019
Nicholas Wilson
Feb 04, 2019
Walter Bright
Feb 05, 2019
Nicholas Wilson
Feb 05, 2019
Walter Bright
Feb 04, 2019
Rubn
Feb 05, 2019
Rubn
Feb 05, 2019
Nicholas Wilson
Feb 05, 2019
Timon Gehr
Feb 05, 2019
Adam D. Ruppe
Feb 05, 2019
Walter Bright
Feb 05, 2019
Rubn
Feb 05, 2019
Walter Bright
Feb 05, 2019
Rubn
Feb 05, 2019
Rubn
Feb 05, 2019
Rubn
Feb 06, 2019
Patrick Schluter
Feb 06, 2019
Rubn
Feb 06, 2019
H. S. Teoh
Feb 06, 2019
jmh530
Feb 06, 2019
H. S. Teoh
Feb 06, 2019
jmh530
Feb 07, 2019
Walter Bright
Feb 07, 2019
Rubn
Feb 07, 2019
Jonathan M Davis
Feb 09, 2019
Rubn
Feb 09, 2019
Jonathan M Davis
Feb 13, 2019
Rubn
Feb 13, 2019
Rubn
Feb 13, 2019
Nicholas Wilson
Feb 18, 2019
Kagamin
Feb 18, 2019
Rubn
Feb 08, 2019
Walter Bright
Feb 09, 2019
Rubn
Feb 08, 2019
Nicholas Wilson
Feb 09, 2019
Rubn
Feb 09, 2019
Norm
Feb 08, 2019
Olivier FAURE
Feb 09, 2019
Rubn
Feb 14, 2019
Olivier FAURE
Feb 07, 2019
Rubn
Feb 06, 2019
Tony
Feb 05, 2019
Dukc
Feb 05, 2019
0xEAB
Feb 05, 2019
Rubn
Feb 05, 2019
Dukc
Feb 06, 2019
Kagamin
February 04, 2019
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?

-Johan

February 04, 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.
February 04, 2019
On Monday, 4 February 2019 at 22:26:08 UTC, Johan Engelen 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?
>
> -Johan

Thats a spec problem. The behaviour is very much deliberate.
February 04, 2019
On 2/4/2019 3:01 PM, Nicholas Wilson wrote:
> Thats a spec problem. The behaviour is very much deliberate.

Which PR was it?
February 04, 2019
On Monday, 4 February 2019 at 23:01:06 UTC, Nicholas Wilson wrote:
> On Monday, 4 February 2019 at 22:26:08 UTC, Johan Engelen 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?
>>
>> -Johan
>
> Thats a spec problem. The behaviour is very much deliberate.

I remember someone pointing out that foreach_reverse didn't implicitly cast to int, but foreach did. Kind of sad they went to opposite way. Very rarely are you going to have an array that will fill a uint. If your type is only 1 byte that's 4 GB of memory. That's not a very common use case. If it's 4 bytes it grows to 16 GB which is more than the average consumer device has. Having to deal with length array being different for 32 and 64-bit is a pain. I ended up creating a function "length32" to avoid that garbage. If I ever have an array that can fill 32-bit then something has gone very wrong (in my use case).
February 05, 2019
On Monday, 4 February 2019 at 23:01:06 UTC, Nicholas Wilson wrote:
> On Monday, 4 February 2019 at 22:26:08 UTC, Johan Engelen 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?
>>
>> -Johan
>
> Thats a spec problem. The behaviour is very much deliberate.

Same goes for "sizeof32" which is especially annoying when sometimes it evaluates to int because the value is known at the time and other times it evaluates to size_t cause it can't determine the value right away or is being used at runtime. If you a struct that can fill a uint something has gone extremely wrong and your executable file is going to be 4 GB minimum to accomodate the .init data.
February 05, 2019
On Monday, 4 February 2019 at 23:30:54 UTC, Walter Bright wrote:
> On 2/4/2019 3:01 PM, Nicholas Wilson wrote:
>> Thats a spec problem. The behaviour is very much deliberate.
>
> Which PR was it?

https://github.com/dlang/dmd/pull/8941
February 05, 2019
On Monday, 4 February 2019 at 23:01:06 UTC, Nicholas Wilson wrote:
> On Monday, 4 February 2019 at 22:26:08 UTC, Johan Engelen 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?
>>
>> -Johan
>
> Thats a spec problem. The behaviour is very much deliberate.

https://github.com/dlang/dlang.org/pull/2565/
February 05, 2019
On 05.02.19 00:01, Nicholas Wilson wrote:
>>
> 
> Thats a spec problem. The behaviour is very much deliberate.

I wish it wasn't. It seems this doesn't do anything useful. If I'm going through the trouble of explicitly specifying the counter type to be what I need, why annoy me with this error message?
February 05, 2019
On Tuesday, 5 February 2019 at 03:15:03 UTC, Timon Gehr wrote:
> I wish it wasn't. It seems this doesn't do anything useful. If I'm going through the trouble of explicitly specifying the counter type to be what I need, why annoy me with this error message?

Ditto.

This change is bewildering to me.
« First   ‹ Prev
1 2 3 4 5