Skip to content
Johann Kerr
Twitter

Navigating 'if' and 'switch' Expressions in Swift 5.9

Code4 min read

In the delightful world of Swift 5.9, there's a smorgasbord of new features to savor. Among the most delectable are the if and switch expressions, which have been seasoned with a dash of flexibility and a pinch of efficiency. Let's take a culinary tour of these syntax enhancements and learn how they can spice up your coding experience!

Sizzling: 'if' and 'switch' as Expressions!

Before Swift 5.9, using if and switch statements often required more steps than preparing a well-marinated steak. You had to create a variable, then use an if or switch statement to assign its value. But now, if and switch can be used directly as expressions! This simplifies your code, much like a well-organized mise en place simplifies cooking.

Let's explore this with a culinary example. Suppose we're organizing a barbecue, and we need to decide what kind of sauce to serve with each type of meat. Previously, we might have done something like this:

let meat = "beef"
var sauce: String
if meat == "beef" {
sauce = "Smoky BBQ"
} else {
sauce = "Spicy Mustard"
}
print(sauce)

But now, with Swift 5.9, we can assign the sauce directly using an if expression:

let meat = "beef"
let sauce = if meat == "beef" { "Smoky BBQ" } else { "Spicy Mustard" }
print(sauce) // Prints "Smoky BBQ"

This is much simpler and cleaner, right? We've reduced our code and made it more concise.

Grilling with 'switch' Expressions

Let's turn up the heat. Suppose we now have a wider variety of meats, and we want to assign sauces based on their specific type. We can use a switch expression to handle this:

let meat = "chicken"
let sauce = switch meat {
case "beef": "Smoky BBQ"
case "pork": "Sweet and Sour"
case "chicken": "Spicy Buffalo"
default: "Garlic Aioli"
}
print(sauce) // Prints "Spicy Buffalo"

With switch expressions, we can easily handle multiple cases without needing nested if statements. Our code remains clean and maintainable, no matter how many types of meat we throw on the grill.

The Surprise Dish: Type Checking

You might think that if expressions now work like the ternary conditional operator (?:), and to some extent, you'd be right. However, the two are not identical. Let's see why with a fun food-related example:

Imagine you're hosting a dinner party, and you need to decide whether to serve steak or a vegetarian option, depending on the dietary preference of your guest. You could use a ternary operator or an if expression to make this decision:

let guestIsVegetarian = false
let decisionWithTernary = guestIsVegetarian ? "Serve the Veggie Burger!" : "Grill the Steak."
let decisionWithIf = if guestIsVegetarian { "Serve the Veggie Burger!" } else { "Grill the Steak." }
print(decisionWithTernary) // Prints "Grill the Steak."
print(decisionWithIf) // Also prints "Grill the Steak

At first glance, the ternary operator and if expression seem identical, but there's a hidden twist! If we were to use different types in our if expression, we'd encounter an issue. Let's assume our decision also impacts the cooking temperature, which is a number:

let decisionWithIf = if guestIsVegetarian { 350 } else { "500°F" } // Error: Different types in different branches

Here, the ternary operator would consider both 350 and "500°F" as similar and automatically convert "500°F" to 500. However, if expressions check the types independently. This means if we use 350 for one case and "500°F" for the other, Swift will complain about it.

let decisionWithTernary = guestIsVegetarian ? 350 : "500°F" // No error

So while if expressions are quite flexible, they demand respect when it comes to types.

The Last Course

Swift 5.9's new if and switch expressions are undeniably robust. They allow for more concise and readable code, reducing boilerplate and making your coding experience smoother. Whether you're deciding on dinner menus or more complex scenarios, these features are here to help.

But remember, every culinary adventure comes with its own set of rules. Always pay attention to type checking when using if expressions to avoid unexpected surprises.

With these new tricks in your chef's hat, you're ready to whip up some truly exceptional code. Happy coding!