[swift-evolution] Code blocks and trailing closures

Rien Rien at Balancingrock.nl
Wed Mar 15 05:35:27 CDT 2017


What does the following code fragment do?

serverCert.write(to: certificateUrl) { showErrorInKeyWindow(message); return }

The only possible answer is: I don’t know.

The problem is finding out what the “return” statement will do.

Without knowing if the {...} is a code block or a trailing closure it is impossible to know what the return statement will do. It will either end the closure or it will end the function that contains this code block.

This could be disambiguated by using the same syntax as for lazy variables:

serverCert.write(to: serverCertificateUrl) { showErrorInKeyWindow(message: message); return }()

Now it is clear that the return statement will only terminate the (trailing) closure.

A question to the educators on the list: Is this a real problem?

Personally, I dislike this situation, but I am also ambivalent towards the solution I just described.

Regards,
Rien

Site: http://balancingrock.nl
Blog: http://swiftrien.blogspot.com
Github: http://github.com/Balancingrock
Project: http://swiftfire.nl







More information about the swift-evolution mailing list