Thread overview
writeln and ~
Jan 14, 2017
Ignacious
Jan 14, 2017
Adam D. Ruppe
Jan 14, 2017
thedeemon
Jan 14, 2017
Era Scarecrow
January 14, 2017
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.



January 14, 2017
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
January 14, 2017
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 ~?

January 14, 2017
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.