三元算子关联 [英] Ternary Operator Associativity

查看:130
本文介绍了三元算子关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在三元运算符的上下文中,我很难理解关联性的概念.在大多数情况下,三元运算符如下所示:

I am having trouble understanding the concept of associativity in the context of ternary operators. In most cases, ternary operators look like this:

a ? b : c

在这种情况下,不需要关联来评估表达式.不过有时,三元运算符是嵌套的:

In this case, no associativity is needed to evaluate the expression. Sometimes though, Ternary Operators are nested:

a ? b : c ? d : e
a ? b : (c ? d : e) // : is right-associative

但是,嵌套也可以反转

a ? b ? c : d : e
a ? (b ? c : d) : e // : is left-associative

解释这种现象的最佳方法是什么?您是否可以认为:运算符的关联性取决于上下文,还是我在这里遗漏了什么?

What is the best way to explain this phenomenon? Could you consider the associativity of the : operator to be context-dependant, or am I missing something here?

例如,在Swift中,要定义自己的三元运算符时,关联性问题就变得很重要.

The associativity question becomes relevant when one wants to define their own ternary operator, for example in Swift:

infix operator ? { associativity right precedence 120 }
infix operator : { associativity left precedence 130 }

推荐答案

三元运算符始终是右关联的

三元运算符是右关联的,正如我们在第一个示例中所看到的(并且正如我们在下面看到的那样,这是我们唯一的选择,如果我们想让相应的if-else块包含除表达式以外的任何内容,评估为布尔值).请注意,您的第二个示例不留任何空间关联性,因此,它没有显示任何三元运算符的左侧关联性的示例.

The ternary operator is always right-associative

The ternary operator is right-associative, as we see in your first example (and as we see below, this is the only choice we have if we want to let the corresponding if-else blocks to contain anything else than expressions that evaluate to booleans). Note that your second example leaves no room associativity, so this does it does not show an example of any left-associativity of the ternary operator.

/* example 1 */
a ? b : c ? d : e 
==> { right-ass. } => a ? b : (c ? d : e), OK
==> { left-ass. } => (a ? b : c) ? d : e
/*                        |___|
                             \ not OK: since then both of these must be 
                               booleans: that is restriction we don't have */

/* example 2 */ 
a ? b ? c : d : e
==> { only-valid-splitup } => a ? (b ? c : d) : e

(a ? b ? c) : d : e
/* |___|
      \ not valid, there's no ? ? ternary operator */

a ? b ? (c : d : e)
/*         |___|
              \ not valid, there's no : : ternary operator */

因此,即使嵌套三元运算符表达式,三元运算符的关联性也是定义明确的.但是请注意,这样做会降低代码的可读性,甚至在

Hence, the ternary operator's associativity is well-defined even if you nest ternary operator expressions. Take care, however, that doing so tends decrease code readability, and it is even outright recommended against this in the Language Guide - Basic Operators

...

...

但是,请谨慎使用三元条件运算符.它的 如果使用过度,简洁会导致难以阅读的代码. 避免 将三元条件运算符的多个实例合并为 一个复合语句.

Use the ternary conditional operator with care, however. Its conciseness can lead to hard-to-read code if overused. Avoid combining multiple instances of the ternary conditional operator into one compound statement.

三元运算符:不是两个一元运算符,而是自己的唯一运算符

三元运算符是一种特殊的运算符,与Swift中的任何其他运算符都没有真正的直接关系.所有其他都属于一元和二元运算符的族.

The ternary operator: not two unary operators, but a unique operator of its own

The ternary operator is a special operator not really directly related to any of the other operators in Swift; all other belong to the families of unary and binary operators.

  • 一元运算符...

  • Unary operators ...

二进制运算符...

三元运算符可对三个目标进行操作.与C一样,Swift也只有一个三元运算符,即三元条件运算符(a?b:c).

Ternary operators operate on three targets. Like C, Swift has only one ternary operator, the ternary conditional operator (a ? b : c).

来自语言指南-基本操作符.

由于只允许在Swift中自定义我们自己的prefix(一元)和infix(二进制)运算符,因此我怀疑您将很难实现自己的 true 三元运算符,因为您仅限于将其用作两个单独的一元infix 运算符?:,这自然不同于单三元运算符. (您可以随时查看内特·库克(Nate Cook)的这篇过时的博客文章,解释了如何通过使用两个二进制运算符来模仿一个三元运算符,但是由于在Swift 3中将消除烦恼,因此我不知道将来的Swift版本是否可以实现.

Since we're only allowed to customly define our own prefix (unary) and infix (binary) operators in Swift, I suspect you will have quite difficult time implementing your own true ternary operator, since you're limited to using it as two separate unary infix operators ? and :, which is naturally not the same as a single ternary operator. (You could always look at this somewhat old blog post by Nate Cook, explaining how to mimic a ternary operator by using two binary operators, however as currying is to be removed in Swift 3, I don't know if this will be possible for future Swift versions).

这篇关于三元算子关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆