UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Is 0 a Double?

Forums > 100 Days of Swift

In this code (Day 12: Optionals), we're given this in the quiz on Nil Coalescing:

let distanceRan: Double? = 0.5
let distance: Double = distanceRan ?? 0

In my opinion, I thought this wouldn't work because 0.5 is a double and '0' is an Integer. Thus, the right and left sides of ?? does not match.

However, this code is valid.

So in Swift, is 0 considered an Integer?

3      

I ran a little test in Playground. It seems 0 is considered an Int?

var zero = 0
if(zero == 0.0){
    print("true")
} else{
    print("false")
}

Compiler: binary operator '==' cannot be applied to operands of type 'Int' and 'Double'

3      

Swift / SwiftUI is a strongly typed language, menaing the type type on either side of an assignment, comparison or as parameters in a function call, have to match.

In this instance, yes 0 is a Double

let distanceRan: Double? = 0.5
let distance: Double = distanceRan ?? 0

Because distance is defined as Double and the 0 on the same line can be widened from an Integer to a Double if the nil-coalescing was applied at the time the definition. That is the compiler determines it needs to widen the type for the 0 (which it is permitted to do) before assigning the value to distance. Ther is no violation of the strongly type rule for Swift/SwiftUI.

In the second case

var zero = 0
if(zero == 0.0){
    print("true")
} else{
    print("false")
}

You have fixed the type of zero to be an Integer in var zero = 0, before comparing it to a Double later on, so it throws up the error.

4      

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

4      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.