News for hackers. Stuff that matters.
Re:Swift System Is Now Open Source by jasamer on Saturday September 26, 2020 @10:00AM (#24598910)
The difference between guard and if isn’t really about optionals. ‘guard x ...’ tells you that after the guard block, ‘x’ is the case. If x is a Boolean, it’s true; if it’s a matched pattern, it has matched, if it’s a ‘if let’, the value is non-nil.

A guard must return if the condition doesn’t match. There’s no such requirement for if.

I also like that unwrapping is explicit in Swift. ‘if/guard let x = optional’ is the syntax for unwrapping. ‘optional == nil’ is the syntax for checking whether an optional is nil. Why would the latter do any unwrapping? The first time I saw that syntax in Kotlin, I thought that it’s weird to add functionality like this to a normal looking if.

There are no comments on this comment yet.