链式比较运算符的语言支持

链式比较运算符的语言支持

本文介绍了链式比较运算符的语言支持(x< y< z)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

发布了问题关于链式比较运算符以及如何它们以不同的语言解释.

A question was posted about chained comparison operators and how they are interpreted in different languages.

链接比较运算符意味着(x < y < z)将被解释为((x < y) && (y < z))而不是((x < y) < z).

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&lt; y&lt; z)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 13:28