问题描述
一个问题发布了关于链式比较运算符以及如何它们被翻译成不同的语言.
A question was posted about chained comparison operators and how they are interpreted in different languages.
链接比较运算符意味着 (x < y < z)
将被解释为 ((x < y) && (y < z))
而不是 ((x .
Chaining comparison operators means that (x < y < z)
would be interpreted as ((x < y) && (y < z))
instead of as ((x < y) < z)
.
对该问题的评论表明,Python、Perl 6 和 Mathematica 支持链接比较运算符,但其他哪些语言支持此功能,为什么不更常见?
The comments on that question show that Python, Perl 6, and Mathematica support chaining comparison operators, but what other languages support this feature and why is it not more common?
快速浏览 Python 文档就会发现该功能至少在 1996 年就有.是否有更多语言没有添加此语法的原因?
A quick look at the Python documentation shows that this feature has been since at least 1996. Is there a reason more languages have not added this syntax?
静态类型语言在类型转换方面会出现问题,但是否还有其他原因导致这种情况不常见?
A statically typed language would have problems with type conversion, but are there other reasons this is not more common?
推荐答案
它应该更常见,但我怀疑不是因为它使解析语言变得更加复杂.
It should be more common, but I suspect it is not because it makes parsing languages more complex.
优点:
- 坚持最小惊喜原则
- 读起来就像教数学
- 减少认知负荷(见前 2 点)
缺点:
- 语言的语法更复杂
- 特殊情况的语法糖
至于为什么不,我的猜测是:
As to why not, my guesses are:
- 语言作者没有想到
- 在值得拥有"的名单上
- 认为它不足以证明实施的合理性
这篇关于链式比较运算符的语言支持(x <y <z)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!