Feature #12263
closedFeature request: &&. operator (shorthand for foo && foo.method)
Description
Ruby 2.3 introduced the &. safe-navigation operator. I'd like to propose a &&. operator which would be shorthand for:
Unlike &., this does not continue the chain if the variable evaluates to false. This would give the following result:
Updated by johnnyshields (Johnny Shields) over 10 years ago
- Subject changed from Feature request: &&. operator (foo&&.method) to Feature request: &&. operator (shorthand for foo && foo.method)
Updated by nobu (Nobuyoshi Nakada) over 10 years ago
- Description updated (diff)
- Status changed from Open to Feedback
&. was considered useful because of try!.
Is method call on an expression which may be false so frequent?
Updated by shevegen (Robert A. Heiler) over 10 years ago
I don't really like it.
matz said that & is the lonely operator because the person is staring
at a dot before, like &.
&&. would be too lonely because now you have 2 people staring at a
dot together. This would make ruby hackers too sad.
In general I do not like the amplification of some tokens; for instance,
@foo is much nicer than @@foo.
I am also sure that, if you add this, people will suggest $$ as well. :)
I also have to admit that I find "x && y" easier to understand than
"x&&.y".
Updated by phluid61 (Matthew Kerwin) over 10 years ago
Robert A. Heiler wrote:
I also have to admit that I find "x && y" easier to understand than
"x&&.y".
But one could argue that the following, which is semantically equivalent to the proposed &&., is harder to understand:
The only question I have is whether it really has enough utility to warrant adding it as a language construct. (And what of the precedent: will |. and ||. be proposed next?)
Updated by matz (Yukihiro Matsumoto) about 10 years ago
Real world use case, please?
Matz.
Updated by dsferreira (Daniel Ferreira) about 10 years ago
Updated by nobu (Nobuyoshi Nakada) about 10 years ago
Updated by nobu (Nobuyoshi Nakada) about 10 years ago
obj.(args) is a syntax sugar for obj.call(args).
Your notation confuses me.
Updated by dsferreira (Daniel Ferreira) about 10 years ago
I opened a new feature request to extend the safe navigation operator. https://bugs.ruby-lang.org/issues/12412
I believe that with my proposal the aim of this feature request can also be achieved.
Updated by dsferreira (Daniel Ferreira) about 10 years ago
By implementing https://bugs.ruby-lang.org/issues/12412
We would get the desired behaviour:
For me the situation:
it is a particular situation that can be handled using extra logic.
I don't think there are enough case situations to justify the addition of a new operator to the language.
And by extending the safe navigation operator we will be making use of an already existent language feature.