I have an enum for tagged values (this is from a toy Scheme interpreter project), like this:
enum Value {
case int(Int)
case float(Float)
case bool(Bool)
...
}
I wrote an initializer for it:
init?(value:Any) {
switch v {
case let v as Int: self = .int(v)
case let v as Float: self = .float(v)
case let v as Bool: self = .bool(v)
...
default: return nil
}
}
With that, calling Value(3)
returns an (optional) Value.int(3), as expected. Bool's and the other types work too.
But Value(0.1)
hits the default case and returns nil
.
(Edited for correctness and recommended style. Thank you everyone for pointing those issues out.)
Explanation: The switch/case statement in the c language is defined by the language specification to use an int value, so you can not use a float value. The value of the 'expression' in a switch-case statement must be an integer, char, short, long. Float and double are not allowed.
Switch case allows only integer and character constants in case expression. We can't use float values.
The switch statement doesn't accept arguments of type long, float, double,boolean or any object besides String.
The default inferred type for floating point constants is Double
, not Float
. It's defined by the swift standard library here: https://developer.apple.com/reference/swift/floatliteraltype using the compiler-known FloatLiteralType
typealias.
The documentation of type inference says:
Swift always chooses
Double
(rather thanFloat
) when inferring the type of floating-point numbers.
If you want to use Float
, you must explicitly cast the literal:
enum Value {
case Int(Int)
case Float(Float)
case Bool(Bool)
case Double(Double)
init?(v:Any) {
switch v {
case let v as Int: self = .Int(v)
case let v as Float: self = .Float(v)
case let v as Bool: self = .Bool(v)
case let v as Double: self = .Double(v)
default: return nil
}
}
}
Value(v: 0.1) //Infer Double > Result: Double(0.10000000000000001)
Value(v: Float(0.0002)) //Specify Float > Result: Float(0.000199999995)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With