Strange implicit conversion integers on concatenation

12345swordy alexanderheistermann at gmail.com
Mon Nov 12 20:38:16 UTC 2018


On Monday, 12 November 2018 at 20:23:42 UTC, Andrei Alexandrescu 
wrote:
> On 11/12/18 3:01 PM, 12345swordy wrote:
>> 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?
>>>
>>> import std.stdio;
>>>
>>> enum TestEnum: ulong {
>>>    Item1 = 2,
>>>    Item3 = 5
>>> }
>>>
>>> void main()
>>> {
>>>     string res = `Number value: ` ~ TestEnum.Item1;
>>>     writeln(res);
>>> }
>>>
>>> Output:
>>> Number value: 
>> 
>> Welp with the recent rejection of the DIP 1005, I don't see 
>> this being deprecated any time soon.
>> 
>> -Alex
>
> If we deprecate that we also need to deprecate:
>
>     string res = `Number value: ` ~ 65;
>
> Not saying we shouldn't, just that there are many implications.
>
>
> Andrei

We could replace the

string res = `Number value: ` ~ 65;

with:

string res = `Number value: ` ~65.ToString();

which makes

string res = `Number value: 65`

Via extension methods with compile time reflection. (Which I am 
very exited to see with your upcoming DIP that overhauls the 
compile time reflection!)

Which display the intent of converting 65 to a literal string 
equivalent.

Alex




More information about the Digitalmars-d mailing list