Swift 'if let' in the World of Kotlin: How to Stop Fearing null and Start Living
\n\nIf you've ever switched from Swift to Kotlin (or vice versa), you've probably encountered the question: "How do I do that if let in Kotlin?". This is one of the most popular queries on Stack Overflow, and for good reason. Both languages are young, both have absorbed the best of functional programming, but working with optionals (nullable) is implemented differently in each.
In this article, we won't just show how to emulate if let. We'll break down the best practices of 2025 that will make your Kotlin code not only safe but also elegant. You'll learn when to use let, how also is better than apply, and why smart cast is your new best friend.
Quick Primer: What is Swift 'if let'?
\n\nFor those who don't know: in Swift, if let is a construct that simultaneously checks if an optional variable contains a value, and if so, unwraps it into a new constant.
// Swift\nif let unwrappedName = optionalName {\n print("Hello, \\(unwrappedName)!")\n} else {\n print("Name is missing")\n}\n\nIt's safe, readable, and prevents accidentally using nil. Kotlin doesn't have this built-in syntax, but it has a whole arsenal of tools that work even better. Let's take a look.
Method #1: Classic 'if' + Smart Cast
\n\nThe most direct equivalent of if let in Kotlin is a regular if with a null check. The Kotlin compiler is smart enough to consider the variable non-null within the if block after the check. This is called smart cast.
// Kotlin — equivalent of Swift 'if let'\nval optionalName: String? = "Alice"\n\nif (optionalName != null) {\n // Here optionalName is automatically cast to String (not String?)\n println("Hello, $optionalName!")\n} else {\n println("Name is missing")\n}\n\nWhy is this great? No extra variables, no closures. The code reads like regular imperative code. However, there is a nuance: smart cast only works for local variables (val). If optionalName is a class property (var), the compiler cannot guarantee it won't change between the check and the usage.
Method #2: The '.let' Function — King of Elegance
\n\nThe let function is the most popular "if let equivalent" in Kotlin. It executes a block of code only if the object is not null. Inside the block, the object is accessible via it (or you can give it a custom name).
// Swift if let → Kotlin .let\noptionalName?.let { name -> \n println("Hello, $name!")\n} ?: println("Name is missing")\n\nNotice the ?: — this is the Elvis operator. It executes the code on the right if the left side turned out to be null. Together with let, this provides a complete equivalent of if-else.
When to use let?
- \n
- When you need to perform a chain of actions with a non-null value \n
- When working with chains of nullable calls (chaining) \n
- When you want to limit the scope of a variable \n
Example with chaining:
\n\ndata class User(val name: String?, val email: String?)\n\nfun processUser(user: User?) {\n user?.let { u ->\n u.name?.let { n -> println("Name: $n") }\n u.email?.let { e -> println("Email: $e") }\n }\n}\n\nMethod #3: '.also' for Side Effects
\n\nThe also function is similar to let, but it returns the object itself, not the result of the lambda. This is convenient when you need to do something with the object (e.g., logging) and continue working with it.