Swift should not allow you to use "return" on an expression that doesn't return anything. This just leads to confusion when reading the code


(Amir Michail) #1

For example, "return f()" where function f doesn't return anything should not be allowed.


(Adrian Zubarev) #2

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

(Nobody1707) #3

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.