[swift-users] Ambiguous parsing of `return`

Jens Alfke jens at mooseyard.com
Mon Jun 6 12:00:10 CDT 2016

Someone on another forum (not directly related to Swift) just mentioned running into a confusing situation where they had added an early `return` to a method for testing purposes, in order to disable the code following it:

func myFunc() {
        // some code
        // more code that is now skipped.

Unexpectedly, the line following the `return` still got executed, so "I couldn't figure out why my cache kept getting zapped.”

Turns out the Swift parser is interpreting this as `return cache.removeAll()`, which works because that expression returns void, which matches the function’s return type.

This seems like a case where the parser is playing by the rules, but the result is not what a human would expect. It would be better for a `return` on a line by itself to be parsed as a complete statement, without continuing to the next line. Is this already a known issue?


PS: I’m sure someone will point out that adding an early return like this is sub-optimal, and the compiler could warn that the code following is unreachable. Which is true, and I use comments to disable code in situations like this. But I’m sure this developer’s not the only one who adds `return` instead.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20160606/8bfa00cf/attachment.html>

More information about the swift-users mailing list