为什么 return 语句会破坏条件运算符?

发布于 2024-12-15 16:21:50 字数 718 浏览 1 评论 0原文

在 ruby​​ 中试验条件运算符,

def nada
  false ? true : nil
end

def err
  false ? true : raise('false')
end

按预期工作,但

def reflection
  false ? true : return false
end

产生语法错误,意外的keyword_false,期望keyword_end

def reflection
  false ? true : return(false)
end

并尝试使用括号语法错误,意外的tLPAREN,期望keyword_end

def reflection
  false ? true : (return false)
end

有效正如预期的那样,更详细的 if...then...else...end

def falsy
  if false then true else return false end
end

也适用正如预期的那样。

那么条件(三元)运算符是怎么回事?

Experimenting with the conditional operator in ruby,

def nada
  false ? true : nil
end

def err
  false ? true : raise('false')
end

work as expected but

def reflection
  false ? true : return false
end

produces a syntax error, unexpected keyword_false, expecting keyword_end

def reflection
  false ? true : return(false)
end

and attempted with brackets syntax error, unexpected tLPAREN, expecting keyword_end

yet

def reflection
  false ? true : (return false)
end

works as expected, and the more verbose if...then...else...end

def falsy
  if false then true else return false end
end

also works as expected.

So what's up with the conditional (ternary) operator?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我不吻晚风 2024-12-22 16:21:50

您可以这样使用它,将整个 return 表达式放在括号中:

def reflection
  false ? true : (return false)
end

当然,这样使用没有多大意义,但由于您正在试验(很好!),所以上面的方法有效!我认为该错误是由于 Ruby 语法的工作方式造成的 - 它需要某种结构来形成有效的表达式。

更新

引用草案规范中的一些信息:

表达式是组成语句的程序结构(参见 12
)。单个表达式可以是一个表达式语句
(见12.2).12

注意表达式和语句之间的区别在于
表达式通常用在需要其值的地方,但是
语句通常用在其值不一定的地方
必需的。不过,也有一些例外。例如,一个
跳转表达式(参见11.5.2.4)没有值,并且该值
可以使用复合语句的最后一个语句。

注意。在上面,jump-expression包括return等等。

You can use it like this, by putting the entire return expression in parentheses:

def reflection
  false ? true : (return false)
end

Of course, it does not make much sense used like this, but since you're experimenting (good!), the above works! The error is because of the way the Ruby grammar works I suppose - it expects a certain structure to form a valid expression.

UPDATE

Quoting some information from a draft specification:

An expression is a program construct which make up a statement (see 12
). A single expression can be a statement as an expression-statement
(see 12.2).12

NOTE A difference between an expression and a statement is that an
expression is ordinarily used where its value is required, but a
statement is ordinarily used where its value is not necessarily
required. However, there are some exceptions. For example, a
jump-expression (see 11.5.2.4) does not have a value, and the value
of the last statement of a compound-statement can be used.

NB. In the above, jump-expression includes return among others.

酒解孤独 2024-12-22 16:21:50

我认为这都与 ruby​​ 解析器有关。

  • ruby 将 return 解析为三元运算符的 else 表达式
  • ,然后,当 ruby​​ 发现 false 而不是 end
  • 包装 return false 时,它​​会感到惊讶括号中的 会导致 ruby​​ 将整个内容解释为 else 表达式
  • return(false) 不起作用,因为 ruby​​ 仍在尝试仅解释 return部分作为 else 表达式,并且感到惊讶当它找到左括号时(更新)

注意:我认为这不是一个很好的答案。

例如,一个很好的答案可以参考 ruby​​ 语法来解释解析错误。

I think this is all related to the ruby parser.

  • ruby parses return as the else-expression of the ternary operator
  • ruby is then surprised when it finds false instead of end
  • wrapping return false in parentheses causes ruby to interpret the entire thing as the else-expression
  • return(false) doesn't work because ruby is still trying to interpret just the return part as the else-expression, and is surprised when it finds a left-parenthesis (updated)

Note: I don't think this is a great answer.

A great answer could, for example, explain the parse errors with reference to the ruby grammar.

无妨# 2024-12-22 16:21:50

ternery 运算符就是一个运算符。你不会从中回来。您从函数中返回。当您在 if 中放入 return 时,您将从 if 所在的函数返回。由于没有变量等待 if 结果的赋值,因此没有问题。当您从 ternery 运算符返回时,没有为变量分配任何值。

The ternery operator is just that, an operator. You don't return from it. You return from functions. When you put a return in an if, you return from the function that the if is in. Since there is no variable awaiting assignment from the result of the if, there is no problem. When you return from the ternery operator, there is no value assigned to the variable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文