www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - writeln and ~

reply Ignacious <I.D.T ProjectMaya.com> writes:
string concatenation is weird.

We can do stuff like

writeln(x);

where x is, say a struct and it prints fine

but when we do

writeln(x ~ " ok");

it fails and requires us to convert x!

Why can't string concatenation automatically try to convert the 
arguments? Is there any reason this is bad behavior?

How bout then having writeln parse the arguments as a string(take 
an alias or something first) and then automatically convert them?

Seems like there is no real excuse to add such obfuscation...

Obviously I can roll my own but that is not an acceptable answer.
Jan 14 2017
next sibling parent Adam D. Ruppe <destructionator gmail.com> writes:
On Saturday, 14 January 2017 at 17:42:05 UTC, Ignacious wrote:
 Why can't string concatenation automatically try to convert the 
 arguments? Is there any reason this is bad behavior?
You'd be liable to write buggy stuff like appending the wrong kind of array since the auto conversion was wrong.
 How bout then having writeln parse the arguments as a 
 string(take an alias or something first) and then automatically 
 convert them?
writeln already knows how to convert. There's also a std.conv.text function that returns the string instead of printing it. http://dpldocs.info/experimental-docs/std.conv.text.html
Jan 14 2017
prev sibling next sibling parent thedeemon <dlang thedeemon.com> writes:
On Saturday, 14 January 2017 at 17:42:05 UTC, Ignacious wrote:
 writeln(x ~ " ok");
Just write writeln(x, " ok"); Any number of arguments, no unnecessary allocations. Do you really need ~?
Jan 14 2017
prev sibling parent Era Scarecrow <rtcvb32 yahoo.com> writes:
On Saturday, 14 January 2017 at 17:42:05 UTC, Ignacious wrote:
 Why can't string concatenation automatically try to convert the 
 arguments? Is there any reason this is bad behavior?
Somehow I think that everything implicitly converting to a string seems like a bad idea. Although writefln and writeln using ,'s seems like better ideas than some minor JavaScript convenience.
Jan 14 2017