www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - std.stdio.writeln

reply "Dennis Ritchie" <dennis.ritchie mail.ru> writes:
Hi.
Why prints only the last element?

import std.stdio;

void main() {

	(1, 2, 3).writeln;	// prints 3
}
Mar 09 2015
next sibling parent "Adam D. Ruppe" <destructionator gmail.com> writes:
On Monday, 9 March 2015 at 14:38:41 UTC, Dennis Ritchie wrote:
 	(1, 2, 3).writeln;	// prints 3
that's not an array, use [1,2,3] for that. What you wrote is a parenthesis expression with comma expressions inside. The comma operator is a bit weird: it works like a semicolon, but in different contexts. So 1,2,3, in this context, means "calculate 1, then calculate 2, then calculate 3, the result is the final one: 3". The 1 and 2 are just discarded because their calculations don't save anything outside.
Mar 09 2015
prev sibling parent reply "John Colvin" <john.loughran.colvin gmail.com> writes:
On Monday, 9 March 2015 at 14:38:41 UTC, Dennis Ritchie wrote:
 Hi.
 Why prints only the last element?

 import std.stdio;

 void main() {

 	(1, 2, 3).writeln;	// prints 3
 }
I think this is a misunderstanding about UFCS. 1.writeln(2, 3); is the same as writeln(1, 2, 3); and the same as (1).writeln(2, 3); but (1, 2, 3).writeln; is completely different. UFCS is only for the first argument. Yet another excellent example of why we should abolish the comma operator in D.
Mar 09 2015
next sibling parent "Dennis Ritchie" <dennis.ritchie mail.ru> writes:
On Monday, 9 March 2015 at 14:42:35 UTC, Adam D. Ruppe wrote:
 On Monday, 9 March 2015 at 14:38:41 UTC, Dennis Ritchie wrote:
 	(1, 2, 3).writeln;	// prints 3
that's not an array, use [1,2,3] for that. What you wrote is a parenthesis expression with comma expressions inside. The comma operator is a bit weird: it works like a semicolon, but in different contexts. So 1,2,3, in this context, means "calculate 1, then calculate 2, then calculate 3, the result is the final one: 3". The 1 and 2 are just discarded because their calculations don't save anything outside.
Thanks. On Monday, 9 March 2015 at 14:48:20 UTC, John Colvin wrote:
 I think this is a misunderstanding about UFCS.
Yes.
Mar 09 2015
prev sibling parent reply ketmar <ketmar ketmar.no-ip.org> writes:
On Mon, 09 Mar 2015 14:48:19 +0000, John Colvin wrote:

 Yet another excellent example of why we should abolish the comma
 operator in D.
or at least warn about it. producing a warning for such expression will=20 ring a bell in programmers head: "compiler is unhappy. i definitely doing=20 something wrong here!"=
Mar 09 2015
parent reply Jonathan M Davis via Digitalmars-d-learn writes:
On Monday, March 09, 2015 15:45:58 ketmar via Digitalmars-d-learn wrote:
 On Mon, 09 Mar 2015 14:48:19 +0000, John Colvin wrote:

 Yet another excellent example of why we should abolish the comma
 operator in D.
or at least warn about it. producing a warning for such expression will ring a bell in programmers head: "compiler is unhappy. i definitely doing something wrong here!"
There's not much point in that. If someone is using the comma operator correctly, then the warnings would be incredibly annoying. And if we value avoiding comma operator-related bugs over letting folks take advantage of its usefulness enough to consider such a warning, we might as well just deprecate the comma operator and move towards removing it from the language. Either it's there and useful but error-prone, or it's gone. In between just annoys both groups. - Jonathan M Davis
Mar 09 2015
parent reply ketmar <ketmar ketmar.no-ip.org> writes:
On Mon, 09 Mar 2015 13:18:58 -0700, Jonathan M Davis via
Digitalmars-d-learn wrote:

 On Monday, March 09, 2015 15:45:58 ketmar via Digitalmars-d-learn wrote:
 On Mon, 09 Mar 2015 14:48:19 +0000, John Colvin wrote:

 Yet another excellent example of why we should abolish the comma
 operator in D.
or at least warn about it. producing a warning for such expression will ring a bell in programmers head: "compiler is unhappy. i definitely doing something wrong here!"
=20 There's not much point in that. If someone is using the comma operator correctly, then the warnings would be incredibly annoying. And if we value avoiding comma operator-related bugs over letting folks take advantage of its usefulness enough to consider such a warning, we might as well just deprecate the comma operator and move towards removing it from the language. Either it's there and useful but error-prone, or it's gone. In between just annoys both groups.
i remember that deprecation was rejected. maybe this is false memory,=20 though. btw, there are legit uses of comma, in c-style `for`, for example. this=20 should be left intact, i think (oh, can c-style `for` be deprecated too?! ).=
Mar 09 2015
parent reply "Meta" <jared771 gmail.com> writes:
On Monday, 9 March 2015 at 22:00:46 UTC, ketmar wrote:
 i remember that deprecation was rejected. maybe this is false 
 memory,
 though.

 btw, there are legit uses of comma, in c-style `for`, for 
 example. this
 should be left intact, i think (oh, can c-style `for` be 
 deprecated too?!
 ).
I think the last time there was a big discussion about this everyone agreed that the comma operator in a for loop was acceptable.
Mar 09 2015
parent reply Jonathan M Davis via Digitalmars-d-learn writes:
On Monday, March 09, 2015 22:29:23 Meta via Digitalmars-d-learn wrote:
 On Monday, 9 March 2015 at 22:00:46 UTC, ketmar wrote:
 i remember that deprecation was rejected. maybe this is false
 memory,
 though.

 btw, there are legit uses of comma, in c-style `for`, for
 example. this
 should be left intact, i think (oh, can c-style `for` be
 deprecated too?!
 ).
I think the last time there was a big discussion about this everyone agreed that the comma operator in a for loop was acceptable.
Yeah. Even Java has that. But IIRC, it was decided that the comma operator in general would be deprecated. I could be wrong though, and it's the sort of thing that could easily be decided and then not actually happen for ages (e.g. it was decided years ago that delete would be deprecated, but it still isn't). - Jonathan M Davis
Mar 10 2015
parent ketmar <ketmar ketmar.no-ip.org> writes:
On Tue, 10 Mar 2015 01:31:39 -0700, Jonathan M Davis via
Digitalmars-d-learn wrote:

 it's the sort of thing that could easily be decided and then not
 actually happen for ages (e.g. it was decided years ago that delete
 would be deprecated, but it still isn't).
sometimes i'm irritated by this, but with `delete`... ah, i hope nobody=20 will remember about for for next 20 years.=
Mar 10 2015