Why does Xcode does not throw compilation error?

47 Views Asked by At
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    0
}

Why does compiler does not throw error even when return keyword is not added while returning built-in data types ?

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    UITableViewCell()
}

Whereas in case of non built in types it throws error : Missing return in a function expected to return

1

There are 1 best solutions below

0
Rudedog On BEST ANSWER

Swift 5.1 added the ability to omit the return keyword for functions with a single-expression. See https://github.com/apple/swift-evolution/blob/master/proposals/0255-omit-return.md

It's got nothing to do with built-in data types vs. non-built-in; in fact I'm not even sure what you mean by that.

If you get the error it's because your function is no longer a single expression.

// This compiles
func foo() -> Int {
  42
}

func bar() -> Int {
  #warning("This won't compile")
  42
}