Can we disallow appending integer to string?

Jonathan M Davis via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Apr 19 10:34:01 PDT 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



More information about the Digitalmars-d-learn mailing list