为什么在Swift中允许添加Int和Float字面量,但不允许添加Int和Float变量?
Why Int and Float literals are allowed to be added, but Int and Float variables are not allowed to do the same in Swift?
我尝试在 Swift 中添加 Int
和 Float
文字,并且编译没有任何错误:
var sum = 4 + 5.0 // sum is assigned with value 9.0 and type Double
但是,当我尝试对 Int
和 Float
变量执行相同操作时,出现了编译时错误,我不得不将任何一个操作数类型转换为另一个操作数的类型让它工作:
var i: Int = 4
var f:Float = 5.0
var sum = i + f // Binary operator '+' cannot be applied to operands of type 'Int' and 'Float'
为什么会这样?它与类型安全有任何关系吗?
如果你想要双重结果:
let i: Int = 4
let f: Float = 5.0
let sum = Double(i) + Double(f)
print("This is the sum:", sum)
如果你想要 Int 结果:
let i: Int = 4
let f: Float = 5.0
let sum = i + Int(f)
print("This is the sum:", sum)
Swift.org 上的文档说:
Type inference is particularly useful when you declare a constant or variable with an initial value. This is often done by assigning a literal value (or literal) to the constant or variable at the point that you declare it. (A literal value is a value that appears directly in your source code, such as 42 and 3.14159 in the examples below.)
For example, if you assign a literal value of 42 to a new constant
without saying what type it is, Swift infers that you want the
constant to be an Int, because you have initialized it with a number
that looks like an integer:
let meaningOfLife = 42 // meaningOfLife is inferred to be of type Int
Likewise, if you don’t specify a type for a floating-point literal,
Swift infers that you want to create a Double:
let pi = 3.14159 // pi is inferred to be of type Double Swift always
chooses Double (rather than Float) when inferring the type of
floating-point numbers.
If you combine integer and floating-point literals in an expression, a
type of Double will be inferred from the context:
> let anotherPi = 3 + 0.14159 // anotherPi is also inferred to be of
type Double The literal value of 3 has no explicit type in and of
itself, and so an appropriate output type of Double is inferred from
the presence of a floating-point literal as part of the addition.
在 var sum = 4 + 5.0
的情况下,编译器会自动将 4 转换为浮点数,因为这是执行操作所需的。
如果您写 var x: Float = 4
,也会发生同样的情况。 4 自动转换为浮点数。
在第二种情况下,由于您已经显式定义了变量的类型,因此编译器没有根据要求自由更改的自由。
解决方法看@Fabio的回答
我尝试在 Swift 中添加 Int
和 Float
文字,并且编译没有任何错误:
var sum = 4 + 5.0 // sum is assigned with value 9.0 and type Double
但是,当我尝试对 Int
和 Float
变量执行相同操作时,出现了编译时错误,我不得不将任何一个操作数类型转换为另一个操作数的类型让它工作:
var i: Int = 4
var f:Float = 5.0
var sum = i + f // Binary operator '+' cannot be applied to operands of type 'Int' and 'Float'
为什么会这样?它与类型安全有任何关系吗?
如果你想要双重结果:
let i: Int = 4
let f: Float = 5.0
let sum = Double(i) + Double(f)
print("This is the sum:", sum)
如果你想要 Int 结果:
let i: Int = 4
let f: Float = 5.0
let sum = i + Int(f)
print("This is the sum:", sum)
Swift.org 上的文档说:
Type inference is particularly useful when you declare a constant or variable with an initial value. This is often done by assigning a literal value (or literal) to the constant or variable at the point that you declare it. (A literal value is a value that appears directly in your source code, such as 42 and 3.14159 in the examples below.)
For example, if you assign a literal value of 42 to a new constant without saying what type it is, Swift infers that you want the constant to be an Int, because you have initialized it with a number that looks like an integer:
let meaningOfLife = 42 // meaningOfLife is inferred to be of type Int
Likewise, if you don’t specify a type for a floating-point literal, Swift infers that you want to create a Double:
let pi = 3.14159 // pi is inferred to be of type Double Swift always
chooses Double (rather than Float) when inferring the type of floating-point numbers.
If you combine integer and floating-point literals in an expression, a type of Double will be inferred from the context:
> let anotherPi = 3 + 0.14159 // anotherPi is also inferred to be of
type Double The literal value of 3 has no explicit type in and of itself, and so an appropriate output type of Double is inferred from the presence of a floating-point literal as part of the addition.
在 var sum = 4 + 5.0
的情况下,编译器会自动将 4 转换为浮点数,因为这是执行操作所需的。
如果您写 var x: Float = 4
,也会发生同样的情况。 4 自动转换为浮点数。
在第二种情况下,由于您已经显式定义了变量的类型,因此编译器没有根据要求自由更改的自由。
解决方法看@Fabio的回答