Numbers in swift

Sincerely, I do not understand what Apple are doing with Swift!!!

It is the only programming language that is incapable to run the code below:

var x: Int = 2

var y: Double = 2.0

var z: Double = y / x

The expected value of z is 1.0.

Any language can make this calculation, except Swift.

This imposes a big difficulty where it should not be.


Int and Double must be freely convertible one to another, at least using casting.


You can not do any thing below:


var k: Double

k = Double(x)

k = Double(x.value)

k = (Double) x

k = (Double) x.value


Serious???

I am very dissapointed with Swift because of it.

It had everything to be a good language to program but it is not because this bug.

Replies

I know many programmers complaining about how Swift treats numeric types, but at least this works:

var x: Int = 2
var k: Double
k = Double(x)

You can not do any thing below:

You may be missing something.

The property .value is just for internal (maybe for Standard Library developers) and Swift does not have C-like casting as `(TypeName)`.

But type conversion through initializer works for Ints and Doubles.

1.0 is not expected; it doesn't have the capacity. You are that which expects this.


That said, you are also incorrect about

var z = y / Double(x)

not working. It does.


I like the lack of bugs that come from forcing explicitness. I also don't like parentheses much, so I do:

protocol DoubleBOWEMOJI {
  var Double: Swift.Double {get}
}
extension Int: DoubleBOWEMOJI {
   var Double: Swift.Double {return Swift.Double(self)}
}

var z = y / x.Double


I use the present emoji if the initializer is failable.