为什么Ruby使用自己的语法来安全导航操作符?

时间:2023-02-06 22:32:59

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

Ruby 2.3.0引入了安全的导航语法,通过引入一个仅在前一个语句的值不为nil时调用该方法的新运算符,简化了对链式方法调用的零处理。这是一个已经存在的功能,例如在C#,Groovy和Swift中。例如在Groovy中,语法是

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.

这基本上意味着结果值是foo.bar的结果值,除非foo为null,在这种情况下返回值也为null,因此不会抛出异常。此外,C#(称为空条件运算符)和Swift(称为可选链接表达式)使用此表示法。

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

所以语法似乎在其他语言中非常标准。现在,为什么在Ruby中语法是

foo&.bar

instead?

1 个解决方案

#1


11  

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.

这个答案是基于对Ruby问题跟踪中的功能请求的讨论。根据Ruby的作者Yukihiro Matsumoto所说,不可能引入运营商?在Ruby中因为foo?是有效的方法名称,因此无法解析。运算符的第一个候选者是反向序列。该语法已经实现(由Nobuyoshi Nakada执行)但后来被丢弃,因为它被认为太接近其他语言引入的原始语法(如前所述,这是不可行的)。最后的语法&。被松本建议接受。

Here's the justification for this syntax given by Matsumoto

以下是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.
  • 。?类似于 ?。在Swift和其他语言中,但无论如何都是不同的。

  • Since ? is a valid suffix of method names in Ruby, we already see a lot of question marks in our programs.
  • 既然?是Ruby中方法名称的有效后缀,我们已经在程序中看到了很多问号。

  • u&.profile reminds us as short form of u && u.profile.
  • u&.profile提醒我们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.

然后,此语法作为Ruby 2.3.0-preview1的一部分发布。

#1


11  

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.

这个答案是基于对Ruby问题跟踪中的功能请求的讨论。根据Ruby的作者Yukihiro Matsumoto所说,不可能引入运营商?在Ruby中因为foo?是有效的方法名称,因此无法解析。运算符的第一个候选者是反向序列。该语法已经实现(由Nobuyoshi Nakada执行)但后来被丢弃,因为它被认为太接近其他语言引入的原始语法(如前所述,这是不可行的)。最后的语法&。被松本建议接受。

Here's the justification for this syntax given by Matsumoto

以下是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.
  • 。?类似于 ?。在Swift和其他语言中,但无论如何都是不同的。

  • Since ? is a valid suffix of method names in Ruby, we already see a lot of question marks in our programs.
  • 既然?是Ruby中方法名称的有效后缀,我们已经在程序中看到了很多问号。

  • u&.profile reminds us as short form of u && u.profile.
  • u&.profile提醒我们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.

然后,此语法作为Ruby 2.3.0-preview1的一部分发布。