21

Ruby 2.3.0 introduces the safe navigation syntax that eases the nil handling of chained method calls by introducing a new operator that only calls the method if value of previous statement is not nil. This is a feature that already exists for example in C#, Groovy and Swift. For example in Groovy, the syntax is

foo?.bar

which basically means that the result value is that of foo.bar unless foo is null, in which case the return value is also null and thus no exception is thrown. Also C# (called null-conditional operators) and Swift (called optional-chaining expression) use this notation.

So the syntax seems to be quite standard in other languages. Now, why in Ruby the syntax is

foo&.bar

instead?

Roope Hakulinen
  • 7,326
  • 4
  • 43
  • 66
  • 5
    `foo?.bar` means "call method `foo?`, then call method `bar` on the result". 2.3.0 is a backwards-compatible release, your proposed syntax would result in **massive** breakage, it would *at least* need a couple of years deprecation warning before it could be introduced. – Jörg W Mittag Nov 16 '15 at 15:59

1 Answers1

37

This answer is based on the discussion of the feature request in Ruby's issue tracking. According to Ruby's author Yukihiro Matsumoto it wouldn't be possible to introduce operator ?. in Ruby because foo? is valid method name and thus it couldn't be parsed. The first candidate for operator was reversed sequence .?. That syntax was already implemented (by Nobuyoshi Nakada) but was later discarded as it was thought to be too close to original syntax introduced by the other languages (that was not feasible as mentioned earlier). The final syntax &. was accepted as suggested by Matsumoto.

Here's the justification for this syntax given by Matsumoto

I think about this for a while, and thinking of introducing &. instead of .?, because:

  • .? is similar to ?. in Swift and other languages, but is different anyway.
  • Since ? is a valid suffix of method names in Ruby, we already see a lot of question marks in our programs.
  • u&.profile reminds us as short form of u && u.profile.

But behavior of &. should be kept, i.e. it should skip nil but recognize false.

This syntax was then released as part of Ruby 2.3.0-preview1.

Community
  • 1
  • 1
Roope Hakulinen
  • 7,326
  • 4
  • 43
  • 66
  • 2
    If you want your answer not to be a meaningless copy-and-paste but a helpful resource, you don't need to cite the discussion verbatim; you should rather at least correct the grammatical mistakes in it (using the editorial marks like `[]`), or better, rephrase it in your own words. It is also not clear what "skip `nil` but recognize `false`" means. Can you make clear what that means? Without it, your question and answer is just a copy of the discussion, and I don't see any value in it. – sawa Nov 16 '15 at 13:43
  • 6
    Matz also explains this same justification for this choice of operator in his RubyConf 2015 keynote talk. It may be more understandable to watch the video than the referenced bug reports. He indicates that the `&.` is called the "lonely operator" (with some imagination, it can be seen to look like a lonely person sitting on the floor staring at the dot in front of them). See slides 78-87, between 15:00 to 17:40 [YouTube keynote video](https://www.youtube.com/watch?v=LE0g2TUsJ4U) – Mark Glossop Nov 30 '15 at 14:12
  • The reasoning that `.?` looks too much like `?.` seems pretty weak – forresthopkinsa Feb 15 '22 at 00:11