Jump to page: 1 2
Thread overview
Can we disallow appending integer to string?
Apr 19, 2017
Nick Treleaven
Apr 19, 2017
Stanislav Blinov
Apr 19, 2017
Jonathan M Davis
Apr 19, 2017
Stanislav Blinov
Apr 19, 2017
H. S. Teoh
Apr 19, 2017
Stanislav Blinov
Apr 20, 2017
Nick Treleaven
Apr 20, 2017
H. S. Teoh
Apr 20, 2017
Stanislav Blinov
Apr 20, 2017
H. S. Teoh
Apr 21, 2017
Nick Treleaven
Apr 21, 2017
Nick Treleaven
Apr 21, 2017
XavierAP
April 19, 2017
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?
April 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).
April 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:
> > 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).

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 Davis

April 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.


April 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:
> > 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.

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.
April 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...
April 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.
April 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.
April 20, 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... :/

April 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"); }());

« First   ‹ Prev
1 2