Strange implicit conversion integers on concatenation
lithium iodate
whatdoiknow at doesntexist.net
Mon Nov 5 16:12:44 UTC 2018
On Monday, 5 November 2018 at 15:36:31 UTC, uranuz wrote:
> Hello to everyone! By mistake I typed some code like the
> following without using [std.conv: to] and get strange result.
> I believe that following code shouldn't even compile, but it
> does and gives non-printable symbol appended at the end of
> string.
> The same problem is encountered even without [enum]. Just using
> plain integer value gives the same. Is it a bug or someone
> realy could rely on this behaviour?
As long as the integral value is statically known to be a valid
code point and fits into the numerical rangle of the char-type
(which plain 'char' in this case), automatic conversion is done.
If you replace the values inside your enum with something bigger
(> 255) or negative, you will see that the compiler doesn't
universally allow all such automatic integral->char conversions.
You can also see this effect when you declare a mutable vs an
immutable integer and try to append it to a regular string, the
mutable one will fail. (anything that can be larger than 1114111
will always fail, as far as I can tell)
Some consider this useful behavior, but it's not uncontroversial.
More information about the Digitalmars-d
mailing list