如何理解Swift5.5中的Int Double转换?

How to understand the Int Double conversion in Swift 5.5?

扩展“Swift编程语言”(Swift 5.5)“整数和浮点数转换”中的示例:

3 + 0.14 // allowed

let three = 3
let rest = 0.14

3 + rest // allowed
0.14 + three // compile error
three + 0.14 // compile error

我不明白为什么最后两行被认为是编译错误。谁能帮忙解释一下?谢谢

有两个基本规则:

  • 可以隐式转换没有类型注释的数字文字如果可能
  • 常量或变量用固定类型初始化,不能改变。浮点文字变为 Double,整数文字变为 Int.

所以 threeInt0.14Double.

3 + rest 有效,因为 3 可以推断为 Double。
但是 0.14 不能推断为 Int 所以最后两行编译失败。