For example, "return f()" where function f doesn't return anything should not be allowed.
In Swift we always return something. In your case you're returning implicitly Void
which is an empty tuple.
func f() {}
func foo() {
return f()
}
This is not ideal but still a useful thing to do sometimes.
// example 1
guard
condition
else { return f() } // You don't have to write `return` on an extra line.
// example 2
var optional: Int?
var foo: Int = 0
optional = 42
// `map` returns `Void?` because `=` operator returns `Void`.
optional.map { foo = $0 } // implicit `return` for simple expressions.
let value = optional.map { return foo = $0 } // explicit `return`
type(of: value) == Void?.self // true
type(of: optional = 100) == Void.self // true
7 Likes
It's especially useful in functions taking a generic function parameter, since you don't have to special case for when you're passed in a (/*whatever*/) -> Void
.
5 Likes