var wth = 0
func SwiftPlease() {
wth += 1
return
wth += 1 // ⚠️ Expression following 'return' is treated as an argument of the 'return'
}
SwiftPlease()
print(wth) // Can you guess what this will be?
I can almost see what's happening here, but why allow an argument for the return at all if the function's signature does not specify a return value?
I‘d be against a warning because I frequently use that in the else body of a guard statement. There is nothing wrong learning that several operators including = return Void and using that in combination with return.
To me this is a programmer mistake, not a bug, since in Swift there is always a return type, but for convenience it could be implicit. Not understanding this fact is a source of bugs.
I’m quite interested as to why you’d do that instead of just using multi-line statements, or is it just purely stylistic choice?
I’d say it’s a rarely-used fact, and most programmer won’t ever need to think in terms of Void-returning function.
Anyhow, I don’t have a strong opinion on this matter. I know of the technicality of it (for a long time actually). I still feel that it’d be hard to read if I actually use it, but that’s about it.
In a lot of async code that take a completion handler, the completion handler is semantically like a (delayed) return value. You need it on every early return path.
guard condition else { return completion(nil) }
…neatly mirrors return nil in a synchronous function.