Code after a RETURN

Has anyone else seen this?
Sometime I will temporaialy bypass a Func by inserting a Return
and most of the time Swift still executes the statement RIGHT after the return

private func test() {
   return
   print("this is test func")
 .... other stuff
}

the print is still executed, but "other stuff" is not

It's not a bug, exactly. Because Swift is generally white-space-agnostic, the print call — which returns a Void result — is an expression that's returned by the return. Since func test implicitly returns Void, and the print expression result is Void, there's no type mismatch and the return happens properly, with the printed output as a side-effect.

The line after the print starts a new statement, so it isn't executed due to the return.

The safest way to return early is to use a terminating ; for this scenario:

private func test() {
   return;
   print("this is test func") // <-- will not be executed
 .... other stuff
}
4 Likes

Thanks...

I had not noticed this warning.. .which basicily confirms what you just said :slight_smile:

Expression following 'return' is treated as an argument of the 'return'
Terms of Service

Privacy Policy

Cookie Policy