美文网首页iOS开发
iOS-Swift中的递增(++)和递减(--)被取消的原因-官

iOS-Swift中的递增(++)和递减(--)被取消的原因-官

作者: Lebus | 来源:发表于2019-04-30 19:57 被阅读94次

众所周知,在很多编程语言中,对一个变量递增1用++,递减1用--,在Swift3之前也是可以这么用的,但之后被取消了。

所以在目前Swift5的版本中,只能用+=1-=1来进行递增和递减了

如果坚持用++--将会提示以下错误:

Use of unresolved operator '++'; did you mean '+= 1'?

Use of unresolved operator '--'; did you mean '-= 1'?

也是好奇为毛Swift把这么好用的语法取消了,之前也有老外咨询过Chris Lattner(Swift语言缔造者),并获得了回复。
最近也是被学生问起这个问题,所以拿出来分享一下,以下是我总结的:

  1. 这是Swift的新功能(我就这么弄,怎么了)
  2. ++也并没比 += 1简洁多少
  3. 别把Swift和别的语言混淆在一起(这,就是个性)
  4. 别的语言的++主要是用在for循环中:for i = 0; i < n; i++ { ... },但Swift语言的for循环可以这么写:for i in 0..<n { ... },所以++就没什么用了
  5. ++读起来不太拟人,感觉没什么意义,不符合Swift语言一贯的超长命名方式,比如x - ++xfoo(++x, x++),没注释的话之后连自己都看不懂。
  6. 作者本人不喜欢++--

一句话:我们不一样!

以下是Chris Lattner答复的原文:

1.These operators increase the burden to learn Swift as a first programming language - or any other case where you don't already know these operators from a different language.

2.Their expressive advantage is minimal - x++ is not much shorter than x += 1.

3.Swift already deviates from C in that the =, += and other assignment-like operations returns Void (for a number of reasons). These operators are inconsistent with that model.

4.Swift has powerful features that eliminate many of the common reasons you'd use ++i in a C-style for loop in other languages, so these are relatively infrequently used in well-written Swift code. These features include the for-in loop, ranges, enumerate, map, etc.

5.Code that actually uses the result value of these operators is often confusing and subtle to a reader/maintainer of code. They encourage "overly tricky" code which may be cute, but difficult to understand.

6.While Swift has well defined order of evaluation, any code that depended on it (like foo(++a, a++)) would be undesirable even if it was well-defined.

7.These operators are applicable to relatively few types: integer and floating point scalars, and iterator-like concepts. They do not apply to complex numbers, matrices, etc.

Finally, these fail the metric of "if we didn't already have these, would we add them to Swift 3?"

 

广告时间:

小弟有做了两套iOS开发的视频教程,欢迎大家来试听:
https://m.cctalk.com/inst/s9vfhehl

相关文章

  • iOS-Swift中的递增(++)和递减(--)被取消的原因-官

    众所周知,在很多编程语言中,对一个变量递增1用++,递减1用--,在Swift3之前也是可以这么用的,但之后被取消...

  • JavaScript操作符

    1.递增与递减操作符 前置递增和递减操作时,变量的值都是在语句被求值之前改变的 后置递增和递减操作是在包含他们的语...

  • 递增和递减的关系

    从前一农夫常年在地里劳作。 这一天,他在自家田里发现了五个七彩琉璃球,琢磨着皇帝老爷也许没有见过这些,如果上交了这...

  • JS运算符

    一、算数运算符 +、-、*、/、%(取余) 9%2=1 二、递增递减运算符 ++、--,递增和递减即可以放在变量前...

  • 10.12嵌入式知识点总结

    1.定时器分为三种高级控制,通用,基本 2.类型分为递增,递减,递增/递减,其中递增/递减为中心对齐 3.分频系数...

  • PHP 中「自增、自减」运算引发的奇怪问题

    在 PHP 的官方手册中写道: PHP 支持 C 风格的前/后递增与递减运算符。 第一个注意事:递增/递减运算符不...

  • 17.单调队列

    递增队列 递减队列

  • ARM栈结构

    ARM 栈类型 根据栈生长方向,ARM的栈可分为递增堆栈和递减堆栈。 递增堆栈:栈向高地址生长 递减堆栈:栈向低地...

  • 数学分析理论基础7:数列极限存在的条件

    数列极限存在的条件 单调数列 定义:若数列的各项满足关系式,则称数列为递增(递减)数列,递增数列和递减数列统称为单...

  • 递增与递减

    (一) “有劳,上茶!” “来了!” “还好!什么茶?” “兰贵人” “好!倒满!” (二) “来!来一杯,兰贵人...

网友评论

    本文标题:iOS-Swift中的递增(++)和递减(--)被取消的原因-官

    本文链接:https://www.haomeiwen.com/subject/hkevnqtx.html