digitalmars.D.learn - Can we disallow appending integer to string?
- Nick Treleaven (9/9) Apr 19 2017 This bug is fixed as the code no longer segfaults but throws
- Stanislav Blinov (3/12) Apr 19 2017 Because integrals implicitly convert to characters of same width
- Jonathan M Davis via Digitalmars-d-learn (12/28) Apr 19 2017 Yeah, which reduces the number of casts required when doing arithmetic o...
- Stanislav Blinov (4/8) Apr 19 2017 Yep, me too. Generally, I don't think that an implicit conversion
- H. S. Teoh via Digitalmars-d-learn (19/27) Apr 19 2017 Me three.
- Stanislav Blinov (11/14) Apr 19 2017 Yup. Not to mention one could have
- Nick Sabalausky (Abscissa) (3/5) Apr 20 2017 Ugh, because apparently doing arithmetic on characters is such a common,...
- Nick Treleaven (18/20) Apr 20 2017 Despite char.min > byte.min, char.max < byte.max. Anyway,
- H. S. Teoh via Digitalmars-d-learn (14/25) Apr 20 2017 +1. Recently I was bitten by the char/int overload ambiguity problem,
- Stanislav Blinov (4/8) Apr 20 2017 Yeah, that one is annoying. I've dealt with this before with:
- H. S. Teoh via Digitalmars-d-learn (5/19) Apr 20 2017 Yeah, wow, that is very annoying.
- Nick Treleaven (10/18) Apr 21 2017 The above mismatches would presumably be handled if we disallow
- Nick Treleaven (3/6) Apr 21 2017 That is in the general case - when VRP can prove an integer fits
- XavierAP (10/15) Apr 21 2017 Huh... I hadn't used but I'd been assuming probably biased from
This bug is fixed as the code no longer segfaults but throws instead: https://issues.dlang.org/show_bug.cgi?id=5995 void main(){ string ret; int i = -1; ret ~= i; } Why is it legal to append an integer?
Apr 19 2017
On Wednesday, 19 April 2017 at 14:36:13 UTC, Nick Treleaven wrote:This bug is fixed as the code no longer segfaults but throws instead: https://issues.dlang.org/show_bug.cgi?id=5995 void main(){ string ret; int i = -1; ret ~= i; } Why is it legal to append an integer?Because integrals implicitly convert to characters of same width (byte -> char, short -> wchar, int -> dchar).
Apr 19 2017
On Wednesday, April 19, 2017 14:50:38 Stanislav Blinov via Digitalmars-d- learn wrote:On Wednesday, 19 April 2017 at 14:36:13 UTC, Nick Treleaven wrote:Yeah, which reduces the number of casts required when doing arithmetic on characters and thus reduces bugs there, and I believe that that's the main reason the implicit conversion from an integral type to a character type exists. So, having the implicit conversion fixes one set of bugs, and disallowing it fixes another. We have similar problems with bool. Personally, I think that we should have taken the stricter approach and not had integral types implicit convert to character types, but from what I recall, Walter feels pretty strongly about the conversion rules being the way that they are. - Jonathan M DavisThis bug is fixed as the code no longer segfaults but throws instead: https://issues.dlang.org/show_bug.cgi?id=5995 void main(){ string ret; int i = -1; ret ~= i; } Why is it legal to append an integer?Because integrals implicitly convert to characters of same width (byte -> char, short -> wchar, int -> dchar).
Apr 19 2017
On Wednesday, 19 April 2017 at 17:34:01 UTC, Jonathan M Davis wrote:Personally, I think that we should have taken the stricter approach and not had integral types implicit convert to character types, but from what I recall, Walter feels pretty strongly about the conversion rules being the way that they are.Yep, me too. Generally, I don't think that an implicit conversion (T : U) should be allowed if T.init is not equivalent to U.init.
Apr 19 2017
On Wed, Apr 19, 2017 at 05:56:18PM +0000, Stanislav Blinov via Digitalmars-d-learn wrote:On Wednesday, 19 April 2017 at 17:34:01 UTC, Jonathan M Davis wrote:Me three. Implicit conversion of int to char/dchar/etc. is a horrible, horrible idea that leads to hard-to-find bugs. The whole point of having a separate type for char vs. ubyte, unlike in C where char pretty much means byte/ubyte, is so that we can keep their distinctions straight, not to continue to confuse them in the typical C manner by having their distinction blurred by implicit casts. Personally, I would rather have arithmetic on char (wchar, dchar) produce results of the same type, so that no implicit conversions are necessary. It seems to totally make sense to me to have to explicitly ask for a character's numerical value -- it documents code intent, which often also helps the programmer clear his head and avoid mistakes that would otherwise inevitably creep in. A few extra keystrokes to type cast(int) or cast(char) ain't gonna kill nobody. In fact, it might even save a few people by preventing certain kinds of bugs. T -- Gone Chopin. Bach in a minuet.Personally, I think that we should have taken the stricter approach and not had integral types implicit convert to character types, but from what I recall, Walter feels pretty strongly about the conversion rules being the way that they are.Yep, me too. Generally, I don't think that an implicit conversion (T : U) should be allowed if T.init is not equivalent to U.init.
Apr 19 2017
On Wednesday, 19 April 2017 at 18:40:23 UTC, H. S. Teoh wrote:A few extra keystrokes to type cast(int) or cast(char) ain't gonna kill nobody. In fact, it might even save a few people by preventing certain kinds of bugs.Yup. Not to mention one could have property auto numeric(Flag!"unsigned" unsigned = No.unsigned, C)(C c) if(isSomeChar!C) { return cast(IntOfSize!(C.sizeof, unsigned))c; } auto v = 'a'.numeric; ...or even have an equivalent as a built-in property of character types...
Apr 19 2017
On 04/19/2017 01:34 PM, Jonathan M Davis via Digitalmars-d-learn wrote:Yeah, which reduces the number of casts required when doing arithmetic on characters and thus reduces bugs there,Ugh, because apparently doing arithmetic on characters is such a common, important use-case in this modern unicode world... :/
Apr 20 2017
On Wednesday, 19 April 2017 at 14:50:38 UTC, Stanislav Blinov wrote:Because integrals implicitly convert to characters of same width (byte -> char, short -> wchar, int -> dchar).Despite char.min > byte.min, char.max < byte.max. Anyway, appending an integer is inconsistent because concatenation is not allowed: int n; string s; s = "" ~ n; //error s ~= n; // ok! This is the same for dchar instead of int. This is also a problem for overloading: alias foo = (char c) => 1; alias foo = (int i) => 4; static assert(foo(7) == 4); // fails I would like to see some small changes to mitigate against things like this, even if we can't agree to prevent the conversion overall. It would be nice if we filed a bug as a rallying point for ideas on this issue.
Apr 20 2017
On Thu, Apr 20, 2017 at 11:05:00AM +0000, Nick Treleaven via Digitalmars-d-learn wrote: [...]This is also a problem for overloading: alias foo = (char c) => 1; alias foo = (int i) => 4; static assert(foo(7) == 4); // fails I would like to see some small changes to mitigate against things like this, even if we can't agree to prevent the conversion overall. It would be nice if we filed a bug as a rallying point for ideas on this issue.+1. Recently I was bitten by the char/int overload ambiguity problem, and it was not pretty. Well, actually, it was worse in my case because I had overloaded char against ubyte. Basically the only way I can disambiguate is to wrap one of them in a struct to force the compiler to treat them differently. Another pernicious thing I encountered recently, related to implicit conversions, is this: https://issues.dlang.org/show_bug.cgi?id=17336 It drew a very enunciated "WAT?!" from me. T -- Gone Chopin. Bach in a minuet.
Apr 20 2017
On Thursday, 20 April 2017 at 19:20:28 UTC, H. S. Teoh wrote:Another pernicious thing I encountered recently, related to implicit conversions, is this: https://issues.dlang.org/show_bug.cgi?id=17336 It drew a very enunciated "WAT?!" from me.Yeah, that one is annoying. I've dealt with this before with: alias OpResult(string op, A, B) = typeof((){ A* a; B* b; return mixin("*a"~op~"*b"); }());
Apr 20 2017
On Thu, Apr 20, 2017 at 10:39:01PM +0000, Stanislav Blinov via Digitalmars-d-learn wrote:On Thursday, 20 April 2017 at 19:20:28 UTC, H. S. Teoh wrote:Yeah, wow, that is very annoying. T -- Debian GNU/Linux: Cray on your desktop.Another pernicious thing I encountered recently, related to implicit conversions, is this: https://issues.dlang.org/show_bug.cgi?id=17336 It drew a very enunciated "WAT?!" from me.Yeah, that one is annoying. I've dealt with this before with: alias OpResult(string op, A, B) = typeof((){ A* a; B* b; return mixin("*a"~op~"*b"); }());
Apr 20 2017
On Thursday, 20 April 2017 at 11:05:00 UTC, Nick Treleaven wrote:On Wednesday, 19 April 2017 at 14:50:38 UTC, Stanislav Blinov wrote:The above mismatches would presumably be handled if we disallow implicit conversion between signed/unsigned (https://issues.dlang.org/show_bug.cgi?id=12919).Because integrals implicitly convert to characters of same width (byte -> char, short -> wchar, int -> dchar).Despite char.min > byte.min, char.max < byte.max.I would like to see some small changes to mitigate against things like this, even if we can't agree to prevent the conversion overall.It seems there's a strong case for preventing the int -> dchar conversion. wchar.max == ushort.max, char.max == ubyte.max, but dchar.max < uint.max. Converting from char types -> integers, whilst (arguably) bug prone, at least is numerically sound. So perhaps we could disallow uint -> dchar, which is unsound.
Apr 21 2017
On Friday, 21 April 2017 at 08:36:12 UTC, Nick Treleaven wrote:Converting from char types -> integers, whilst (arguably) bug prone, at least is numerically sound. So perhaps we could disallow uint -> dchar, which is unsound.That is in the general case - when VRP can prove an integer fits within a dchar, by the logic of this proposal, it is allowed.
Apr 21 2017
On Wednesday, 19 April 2017 at 14:50:38 UTC, Stanislav Blinov wrote:On Wednesday, 19 April 2017 at 14:36:13 UTC, Nick TreleavenHuh... I hadn't used but I'd been assuming probably biased from str ~ i would be equivalent to str ~ to!string(i) instead of str ~ cast(char) i Now I see the problem too...Why is it legal to append an integer?Because integrals implicitly convert to characters of same width (byte -> char, short -> wchar, int -> dchar).
Apr 21 2017