Project

General

Profile

Actions

Feature #6222

closed

Use ++ to connect statements

Added by gcao (Guoliang Cao) over 12 years ago. Updated over 12 years ago.

Status:
Rejected
Target version:
-
[ruby-core:43858]

Description

I propose to use ++ to connect two or multiple statements, e.g.

do_this ++ do_that ++ do_something

It is equivalent to (do_this; do_that; do_something) but more readable.

It can be used to replace below idiom

do_something and return if condition

with

do_something ++ return if condition

The current way is very error prone because do_something might return false/nil in some cases and cause problems. And new Ruby programmers might not understand the meaning behind this idiom and mimic it blindly.

I noticed someone proposed 'then' for same purpose (See http://bugs.ruby-lang.org/issues/6201) but is postponed to 3.0 because it is already a keyword. I like 'then' too but if it is not acceptable due to backward compatibility issue, then '++' is a good alternative.


Related issues 1 (0 open1 closed)

Is duplicate of Ruby master - Feature #6201: do_something then return :special_case (include "then" operator)Rejectedmatz (Yukihiro Matsumoto)03/26/2012Actions

Updated by rosenfeld (Rodrigo Rosenfeld Rosas) over 12 years ago

This seems like a workaround to me. But one that I don't like because Ruby will have to keep supporting this even if/after 'then' is accepted.

The problem is that while I find "then" readable to me, I don't think '++' is obvious to someone reading such statements. Specially when i++ has already a very known behavior in most other languages...

Updated by mame (Yusuke Endoh) over 12 years ago

  • Status changed from Open to Assigned
  • Assignee set to matz (Yukihiro Matsumoto)

Hello,

I would be even happier if you respond that ticket instead
of adding a new one...

Of course, '++' also causes compatibility issue :-)

p 1 ++ 1 #=> 2

'++' may be confusing because it is used for string/list
concatenate operation in some languages, such as Haskell
and Erlang. It looks a method to me.

--
Yusuke Endoh

Updated by gcao (Guoliang Cao) over 12 years ago

Sorry I didn't know this. I have never seen ++ in ruby code (except probably in books) and that made me think it is not valid syntax. Wonder what is the usefulness of '1 + +1' though.

Updated by rosenfeld (Rodrigo Rosenfeld Rosas) over 12 years ago

He didn't say it was useful. He just stated that the argument for allowing it in Ruby 2.0 is not valid because it would also break compatibility just like 'then'. Also, he is advicing you to suggest new syntax to 'then' in that other ticket instead of creating new ones.

By the way, it works in Ruby because the parser will read "a ++ 2" the same way as "a++2", "a + +2" or "a + + 2". Ruby allows one to specify a number with its signal like "-3" or "+5", so "+" is not the plus operator in those representations. It doesn't mean someone actually write "+2" in real code...

Updated by gcao (Guoliang Cao) over 12 years ago

I agree.

If 'then' or '++' does not work, what about 'also'? I'm not a native English speaker and not sure whether it sounds natural. Maybe someone can come up with something that fits well to this case.

do_this also do_that also return if condition

Yusuke, feel free to close this ticket if you prefer and we can continue our discussion in the other one.

Updated by rosenfeld (Rodrigo Rosenfeld Rosas) over 12 years ago

Please do any other name suggestions in the other ticket as suggested by Yusuke.

Updated by trans (Thomas Sawyer) over 12 years ago

What's wrong with:

(do_this; do_that; return) if condition

or perhaps you meant

do_this; do_that; return if condition

Updated by mame (Yusuke Endoh) over 12 years ago

'also' is worse than '++'.
Adding a new keyword causes significant compatibility issue.

def also
...
end

I guess such a method name might be actually used in DSLs.
It also might be used as a flag variable.

Well, I've already added `duplicates' tag to this ticket.
So you can comment to which ever one you like.

Note that scattering discussion logs may take you at a
disadvantage, e.g., your opinion may be missed, our
discussion may go around in circles, etc.

Thomas, see #6201. Right before I tell :-)

--
Yusuke Endoh

Actions #9

Updated by matz (Yukihiro Matsumoto) over 12 years ago

  • Status changed from Assigned to Rejected

Both "then" and "++" could compatibility problem. Your "a then b" or "a ++ b" can be implemented by "(a; b)" without any addition to the language.

Matz.

Updated by gcao (Guoliang Cao) over 12 years ago

Even though using 'also' means adding a new keyword and breaks compatibility, practically no one uses it as method name or variable name. I searched my whole ruby lib directory, it never appeared in code.

Another candidate is \. It doesn't break compatibility and matches the use of \ in current syntax.

Since we use \ to concatenate parts to a statement, \ can be used to concatenate statements to a statement group, assuming (a; b) is called a statement group. E.g.

render \ return if condition

Updated by rosenfeld (Rodrigo Rosenfeld Rosas) over 12 years ago

Guoliang, my issue with "(a; return b) if condition?" is that I don't find it readable and would prefer to read "a then b if condition?" instead. But I don't think that "a \ return if condition?" is more readable than the currently allowed approach. In fact I prefer the current one to "\" which is very unintuitive.

This is not a matter of just finding some operator that wouldn't break backward compatibilities. This is about readability.

Updated by gcao (Guoliang Cao) over 12 years ago

You are right, anything other than 'then' or maybe 'also' hurts readability. Guess I'm too obsessed with finding something that can work for this case :-P

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0