Thread overview | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
March 09, 2015 std.stdio.writeln | ||||
---|---|---|---|---|
| ||||
Hi. Why prints only the last element? import std.stdio; void main() { (1, 2, 3).writeln; // prints 3 } |
March 09, 2015 Re: std.stdio.writeln | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dennis Ritchie | 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.
|
March 09, 2015 Re: std.stdio.writeln | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dennis Ritchie | 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.
|
March 09, 2015 Re: std.stdio.writeln | ||||
---|---|---|---|---|
| ||||
Posted in reply to John Colvin | 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. |
March 09, 2015 Re: std.stdio.writeln | ||||
---|---|---|---|---|
| ||||
Posted in reply to John Colvin Attachments: | 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!"
|
March 09, 2015 Re: std.stdio.writeln | ||||
---|---|---|---|---|
| ||||
Posted in reply to ketmar | 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
|
March 09, 2015 Re: std.stdio.writeln | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan M Davis Attachments: | 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!"
>
> 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, 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?! ).
|
March 09, 2015 Re: std.stdio.writeln | ||||
---|---|---|---|---|
| ||||
Posted in reply to ketmar | 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.
|
March 10, 2015 Re: std.stdio.writeln | ||||
---|---|---|---|---|
| ||||
Posted in reply to Meta | 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
|
March 10, 2015 Re: std.stdio.writeln | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan M Davis Attachments: | 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 will remember about for for next 20 years.
|
Copyright © 1999-2021 by the D Language Foundation