Please note! Don't focus on the zero!
You could use other numbers and have the same results.
let cosmicNumber: Double? = nil
let finalAnswer: Double = cosmicNumber ?? 42 // <-- Do you think 42 is an integer? or a double?
Declared Intentions
As @greenAmber noted, the Swift compiler doesn't evaluate the number, per se. The compiler sees that you have declared the constant finalAnswer
as a Double
. Therefore, the compiler will cast the 42 as a double
, as this was your declared intention.
Undeclared Intentions
But, in this next example the compiler considers this to be an integer
, based on its best guess rules. You did not explicitly define what the var zero
should be.
var zero = 0 // <-- What are your intentions? Unclear. Swift compiler decides zero will contain an Int.
Pro Tip: How to Check
Here's a pro tip. If you're unsure what a variable's type is, ask the compiler. You'll probably find this technique very useful along your SwiftUI learning journey.
See -> What Type is This