Skip to Content
Course content

5: Basic Types and Type Inference

Click on the "Edit" button in the top corner of the screen to edit your slide content.

I see this specific error constantly when developers move to Swift from languages like JavaScript or Python. You’re writing a simple piece of logic, it looks perfectly fine to the human eye, but the compiler throws a fit. Take a look at this snippet for a shopping cart calculation:

let itemQuantity = 3
let pricePerItem = 12.99
let totalCost = itemQuantity * pricePerItem 
// Error: Binary operator '*' cannot be applied to operands of type 'Int' and 'Double'

The 'Binary Operator' Headache

At first glance, this looks like a non-issue. You have an integer (3) and a decimal (12.99). In most languages, the computer just says, "Okay, I'll treat the 3 as a 3.0 and do the math." But Swift is strictly typed. It refuses to guess your intentions.

The variable itemQuantity was inferred as an Int because you gave it a whole number. The variable pricePerItem was inferred as a Double because of the decimal. In Swift, an Int is not a Double, and they cannot interact directly. The compiler isn't being difficult; it's preventing a whole category of precision bugs that plague other languages.

Explicit Casting for Type Safety

To fix this, you have to be explicit about the conversion. You need to tell Swift to treat the integer as a double for this specific calculation. Here is how we resolve it:

let itemQuantity = 3
let pricePerItem = 12.99
let totalCost = Double(itemQuantity) * pricePerItem
// Result: 38.97

By wrapping itemQuantity in Double(), you're creating a temporary double version of that number. Now both sides of the multiplication operator are the same type, and the compiler is happy.

How Swift Guesses Your Types

You might have noticed that I didn't write let itemQuantity: Int = 3. I just wrote let itemQuantity = 3. This is Type Inference. Swift is smart enough to look at the value on the right and assign the type automatically.

Here are the basic types you'll be using 90% of the time:

  • Int: Whole numbers (e.g., 42, -7).
  • Double: Floating-point numbers for high precision (e.g., 3.14159).
  • String: Text wrapped in double quotes (e.g., "Swift is cool").
  • Bool: Boolean values, either true or false.

While inference is great for keeping code clean, there are times when you should provide a Type Annotation. For example, if you want a number to be a Double even though it's currently a whole number, you can do this:

let distance: Double = 10 
// Without the ': Double', Swift would have made this an Int.

I usually rely on inference whenever possible to reduce noise, but if you're calling a function that strictly requires a Double, annotating your variable from the start saves you from having to cast it later.




📋 Practical Task

Building a Simple Trip Fuel Calculator

Your task is to create a small program that calculates the total cost of gas for a road trip. You'll need to handle different basic types and ensure they can interact without causing compiler errors.

Requirements:

  • Create a variable for distanceMiles as an Int (e.g., 450).
  • Create a variable for milesPerGallon as a Double (e.g., 25.5).
  • Create a variable for pricePerGallon as a Double (e.g., 3.89).
  • Calculate the totalCost. You will need to divide the distance by the MPG and then multiply by the price. Remember: You cannot divide an Int by a Double.
  • Print the final result to the console.
Rating
0 0

There are no comments for now.

to be the first to leave a comment.