www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - write multiple lines without "\n" with writeln

reply "Suliman" <evermind live.ru> writes:
In dco source code I have found:

void ShowUsage()
{
writeln("
dco build tool " ~ strVersion ~ "
written by FrankLIKE.
Usage:
dco [<switches...>] <files...>
for example: dco
or: dco app.d
build for dfl2: dco
....
}

I do not see here any "\n". Why this code is output all one by 
line, and not in single line?

Why my code:
writeln("App name:" ~ appName ~
"App version:" ~ appVersion ~
"To see help please use -h\\-help option");
writeln(args);

output:
App name:CoolAppApp version:0.0.1To see help please use -h\-help 
option[".\\app1
.exe"]
Nov 20 2014
parent reply "uri" <uri.grill gmail.com> writes:
On Thursday, 20 November 2014 at 08:28:11 UTC, Suliman wrote:
 In dco source code I have found:

 void ShowUsage()
 {
 writeln("
 dco build tool " ~ strVersion ~ "
 written by FrankLIKE.
 Usage:
 dco [<switches...>] <files...>
 for example: dco
 or: dco app.d
 build for dfl2: dco
 ....
 }

 I do not see here any "\n". Why this code is output all one by 
 line, and not in single line?

 Why my code:
 writeln("App name:" ~ appName ~
 "App version:" ~ appVersion ~
 "To see help please use -h\\-help option");
 writeln(args);

 output:
 App name:CoolAppApp version:0.0.1To see help please use 
 -h\-help option[".\\app1
 .exe"]
"In all string literal forms, an EndOfLine is regarded as a single \n character." http://dlang.org/lex.html It's by design but is *really* annoying and should be limited to WysiwygString literals IMO. Auto-formatting with clang-format or astyle can mess up writeln formatting completely. Cheers, uri
Nov 20 2014
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
uri:

 It's by design
And it's a nice handy design. Bye, bearophile
Nov 20 2014
next sibling parent reply "Suliman" <evermind live.ru> writes:
I understand it.

I expect what concatenation symbol will stay new line in new line 
and not append it's to current:

	writeln(
	"first string"
	"second" ~
	"string"
	);

I expect:
first string
second"
string"

but not:
first   stringsecondstring
Nov 20 2014
next sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
Suliman:

 I understand it.

 I expect what concatenation symbol will stay new line in new 
 line and not append it's to current:

 	writeln(
 	"first string"
 	"second" ~
 	"string"
 	);

 I expect:
 first string
 second"
 string"

 but not:
 first   stringsecondstring
If I compile and run this program: void main() { import std.stdio; writeln( "first string" "second" ~ "string" ); } I see this output (and it's correct, as expected): first stringsecondstring Bye, bearophile
Nov 20 2014
prev sibling parent "Adam D. Ruppe" <destructionator gmail.com> writes:
On Thursday, 20 November 2014 at 11:08:24 UTC, Suliman wrote:
 	writeln(
 	"first string"
 	"second" ~
 	"string"
 	);

 I expect:
 first string
 second"
 string"
There's no quoted newline there. You could do: writeln( "first string second string"); with the newlines enclosed in the quotes, then you'd get what you want. (There's no extra indenting in my example because the indent would show up too when inside quotes) But if it isn't in the quotes, newlines are basically ignored by the compiler like any other whitespace in the code.
Nov 20 2014
prev sibling parent reply "uri" <uri.grill gmail.com> writes:
On Thursday, 20 November 2014 at 10:41:24 UTC, bearophile wrote:
 uri:

 It's by design
And it's a nice handy design. Bye, bearophile
For Wysiwyg strings I agree that it's great but I prefer C/C++/Python like behaviour for double quoted strings. I guess it's what I'm used to :) Cheers, uri
Nov 20 2014
parent reply Ary Borenszweig <ary esperanto.org.ar> writes:
On 11/20/14, 9:05 AM, uri wrote:
 On Thursday, 20 November 2014 at 10:41:24 UTC, bearophile wrote:
 uri:

 It's by design
And it's a nice handy design. Bye, bearophile
For Wysiwyg strings I agree that it's great but I prefer C/C++/Python like behaviour for double quoted strings. I guess it's what I'm used to :) Cheers, uri
In Crystal we chose the following: you can have two consecutive string literals but only if they are separated by a `\` at the end of the line. So this is a syntax error: foo("bar" "baz") But this is ok: foo("bar" \ "baz") Likewise, this is an error: But this is ok: ["foo", "bar" \ "baz", "qux"] This way you avoid silly typing mistakes while at the same time you allow splitting a string across several lines without having to concatenate them at runtime.
Nov 20 2014
parent reply ketmar via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
On Thu, 20 Nov 2014 14:23:23 -0300
Ary Borenszweig via Digitalmars-d-learn
<digitalmars-d-learn puremagic.com> wrote:

 This way you avoid silly typing mistakes while at the same time you=20
 allow splitting a string across several lines without having to=20
 concatenate them at runtime.
i bet that current D frontend is able to concatenate string literals in compile time.
Nov 21 2014
parent reply "Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm gmx.net> writes:
On Friday, 21 November 2014 at 15:00:31 UTC, ketmar via 
Digitalmars-d-learn wrote:
 On Thu, 20 Nov 2014 14:23:23 -0300
 Ary Borenszweig via Digitalmars-d-learn
 <digitalmars-d-learn puremagic.com> wrote:

 This way you avoid silly typing mistakes while at the same 
 time you allow splitting a string across several lines without 
 having to concatenate them at runtime.
i bet that current D frontend is able to concatenate string literals in compile time.
AFAIK yes. There was a change to guarantee that string literals concatenated by ~ are joined at compile time. The goal was to deprecated concatenation by juxtaposition, which hasn't happened yet, though.
Nov 21 2014
parent reply Ary Borenszweig <ary esperanto.org.ar> writes:
On 11/21/14, 1:59 PM, "Marc Schütz" <schuetzm gmx.net>" wrote:
 On Friday, 21 November 2014 at 15:00:31 UTC, ketmar via
 Digitalmars-d-learn wrote:
 On Thu, 20 Nov 2014 14:23:23 -0300
 Ary Borenszweig via Digitalmars-d-learn
 <digitalmars-d-learn puremagic.com> wrote:

 This way you avoid silly typing mistakes while at the same time you
 allow splitting a string across several lines without having to
 concatenate them at runtime.
i bet that current D frontend is able to concatenate string literals in compile time.
AFAIK yes. There was a change to guarantee that string literals concatenated by ~ are joined at compile time. The goal was to deprecated concatenation by juxtaposition, which hasn't happened yet, though.
What's concatenation by juxtaposition?
Nov 21 2014
parent reply "Adam D. Ruppe" <destructionator gmail.com> writes:
On Friday, 21 November 2014 at 17:43:27 UTC, Ary Borenszweig 
wrote:
 What's concatenation by juxtaposition?
When "foo" "bar" turns into "foobar". The two string literals are right next to each other, no operator or anything else in between, so they are combined.
Nov 21 2014
parent Ary Borenszweig <ary esperanto.org.ar> writes:
On 11/21/14, 2:46 PM, Adam D. Ruppe wrote:
 On Friday, 21 November 2014 at 17:43:27 UTC, Ary Borenszweig wrote:
 What's concatenation by juxtaposition?
When "foo" "bar" turns into "foobar". The two string literals are right next to each other, no operator or anything else in between, so they are combined.
Ah, I see. Yes, I guess that's a bug-prone thing to have. And since there's already `~` to concatenate strings (even at compile time) removing that feature would be good.
Nov 21 2014