digitalmars.D - is this considered inconsistency?
- mw (18/18) Mar 21 2021 https://run.dlang.io/is/VOGlGN
- Steven Schveighoffer (3/26) Mar 21 2021 That would be the least useful behavior.
- mw (5/32) Mar 21 2021 It's useful (to be consistent): it reduces user's burden to write
- Dennis (13/15) Mar 21 2021 In `foreach (i; 0 .. -1)`, `i` is typed `int`. In a slice, the
- mw (4/14) Mar 21 2021 Indeed, this shouldn't be allowed, bite by this a number of
- Paul Backus (4/9) Mar 21 2021 There have been proposals to change the integer promotion rules
- mw (13/23) Mar 21 2021 "compatible with C" as a goal of D?
- mw (3/28) Mar 21 2021 Typo:
- Imperatorn (5/29) Mar 23 2021 It would probably have to be the other way around though. Begin
https://run.dlang.io/is/VOGlGN void main() { import std.stdio: writeln; { // example from "Introduction to Algorithms" Cormen et al., p 146 int[] a = [ 4, 1, 3, 2, 16, 9, 10, 14, 8, 7 ]; foreach (i; 0 .. 1 ) writeln(i); // 0 foreach (i; 0 .. 0 ) writeln(i); // no output foreach (i; 0 .. -1 ) writeln(i); // no output foreach (i; a[0 .. 1]) writeln(i); // 4 foreach (i; a[0 .. 0]) writeln(i); // no output foreach (i; a[0 .. -1]) writeln(i); // Range violation } } Why can't the a[0 .. -1] just return empty range, when end <= start, as in a[0 .. 0]? and also in foreach (i; 0 .. -1 )?
Mar 21 2021
On 3/21/21 11:55 AM, mw wrote:https://run.dlang.io/is/VOGlGN void main() { import std.stdio: writeln; { // example from "Introduction to Algorithms" Cormen et al., p 146 int[] a = [ 4, 1, 3, 2, 16, 9, 10, 14, 8, 7 ]; foreach (i; 0 .. 1 ) writeln(i); // 0 foreach (i; 0 .. 0 ) writeln(i); // no output foreach (i; 0 .. -1 ) writeln(i); // no output foreach (i; a[0 .. 1]) writeln(i); // 4 foreach (i; a[0 .. 0]) writeln(i); // no output foreach (i; a[0 .. -1]) writeln(i); // Range violation } } Why can't the a[0 .. -1] just return empty range, when end <= start, as in a[0 .. 0]? and also in foreach (i; 0 .. -1 )?That would be the least useful behavior. -Steve
Mar 21 2021
On Sunday, 21 March 2021 at 16:21:44 UTC, Steven Schveighoffer wrote:On 3/21/21 11:55 AM, mw wrote:It's useful (to be consistent): it reduces user's burden to write the bound check explicitly (verbosely), i.e foreach (i; a[0 .. max(0, end)]) {...}https://run.dlang.io/is/VOGlGN void main() { import std.stdio: writeln; { // example from "Introduction to Algorithms" Cormen et al., p 146 int[] a = [ 4, 1, 3, 2, 16, 9, 10, 14, 8, 7 ]; foreach (i; 0 .. 1 ) writeln(i); // 0 foreach (i; 0 .. 0 ) writeln(i); // no output foreach (i; 0 .. -1 ) writeln(i); // no output foreach (i; a[0 .. 1]) writeln(i); // 4 foreach (i; a[0 .. 0]) writeln(i); // no output foreach (i; a[0 .. -1]) writeln(i); // Range violation } } Why can't the a[0 .. -1] just return empty range, when end <= start, as in a[0 .. 0]? and also in foreach (i; 0 .. -1 )?That would be the least useful behavior.
Mar 21 2021
On Sunday, 21 March 2021 at 15:55:04 UTC, mw wrote:Why can't the a[0 .. -1] just return empty range, when end <= start, as in a[0 .. 0]? and also in foreach (i; 0 .. -1 )?In `foreach (i; 0 .. -1)`, `i` is typed `int`. In a slice, the indices are of type `size_t` which is an unsigned integer type. -1 underflows to `ulong.max` (on 64-bit) which is why you get a range violation. You could define your own slice type as a struct with operator overloads, where you can use signed indices or automatic clamping of the bounds or whatever you want. Changing the behavior of the language type would be a breaking and possibly controversial change. There's also a point to be made that the implicit conversion from signed to unsigned integers should not be allowed, that's a separate discussion.
Mar 21 2021
On Sunday, 21 March 2021 at 16:47:41 UTC, Dennis wrote:On Sunday, 21 March 2021 at 15:55:04 UTC, mw wrote:Ahh, This explains.Why can't the a[0 .. -1] just return empty range, when end <= start, as in a[0 .. 0]? and also in foreach (i; 0 .. -1 )?In `foreach (i; 0 .. -1)`, `i` is typed `int`. In a slice, the indices are of type `size_t` which is an unsigned integer type. -1 underflows to `ulong.max` (on 64-bit) which is why you get a range violation.There's also a point to be made that the implicit conversion from signed to unsigned integers should not be allowed, that's a separate discussion.Indeed, this shouldn't be allowed, bite by this a number of times. Is there a DIP on this already?
Mar 21 2021
On Sunday, 21 March 2021 at 17:01:27 UTC, mw wrote:There have been proposals to change the integer promotion rules in the past, but they've been rejected because Walter wants to keep them compatible with C, to make porting C code to D easier.There's also a point to be made that the implicit conversion from signed to unsigned integers should not be allowed, that's a separate discussion.Indeed, this shouldn't be allowed, bite by this a number of times. Is there a DIP on this already?
Mar 21 2021
On Sunday, 21 March 2021 at 21:22:16 UTC, Paul Backus wrote:On Sunday, 21 March 2021 at 17:01:27 UTC, mw wrote:"compatible with C" as a goal of D? Then why we need D? C++ has been there already 😎 Instead of let a few people decide, how about also let the users decide? I have read -dipxxx switches are kind of previews (not formally in to the language). How about add a -dip-no-explicit-conversion preview, but ask the users permission to send their actuall complier build flags (of their daily work) back to dlang.org, and let that statistics be part of the decision factor? E.g, if 99.999% of the users always have that flag turned on, why not have that feature into the language?There have been proposals to change the integer promotion rules in the past, but they've been rejected because Walter wants to keep them compatible with C, to make porting C code to D easier.There's also a point to be made that the implicit conversion from signed to unsigned integers should not be allowed, that's a separate discussion.Indeed, this shouldn't be allowed, bite by this a number of times. Is there a DIP on this already?
Mar 21 2021
On Sunday, 21 March 2021 at 22:56:43 UTC, mw wrote:On Sunday, 21 March 2021 at 21:22:16 UTC, Paul Backus wrote:Typo: -dip-no-implicit-conversionOn Sunday, 21 March 2021 at 17:01:27 UTC, mw wrote:"compatible with C" as a goal of D? Then why we need D? C++ has been there already 😎 Instead of let a few people decide, how about also let the users decide? I have read -dipxxx switches are kind of previews (not formally in to the language). How about add a -dip-no-explicit-conversion preview, but ask the users permission to send their actuall complier build flags (of their daily work) back to dlang.org, and let that statistics be part of the decision factor? E.g, if 99.999% of the users always have that flag turned on, why not have that feature into the language?There have been proposals to change the integer promotion rules in the past, but they've been rejected because Walter wants to keep them compatible with C, to make porting C code to D easier.There's also a point to be made that the implicit conversion from signed to unsigned integers should not be allowed, that's a separate discussion.Indeed, this shouldn't be allowed, bite by this a number of times. Is there a DIP on this already?
Mar 21 2021
On Sunday, 21 March 2021 at 22:59:26 UTC, mw wrote:On Sunday, 21 March 2021 at 22:56:43 UTC, mw wrote:It would probably have to be the other way around though. Begin by having a --allow-implicit-conversion, then after x amount of time it could possibly be default. But this is a major thing so I doubt itOn Sunday, 21 March 2021 at 21:22:16 UTC, Paul Backus wrote:Typo: -dip-no-implicit-conversion[...]"compatible with C" as a goal of D? Then why we need D? C++ has been there already 😎 Instead of let a few people decide, how about also let the users decide? I have read -dipxxx switches are kind of previews (not formally in to the language). How about add a -dip-no-explicit-conversion preview, but ask the users permission to send their actuall complier build flags (of their daily work) back to dlang.org, and let that statistics be part of the decision factor? E.g, if 99.999% of the users always have that flag turned on, why not have that feature into the language?
Mar 23 2021