[swift-evolution] Move placement of 'throws' statement

thislooksfun thislooksfun at repbot.org
Mon Dec 26 11:38:31 CST 2016

Hello Swifters,

I've been writing a lot more Swift code recently, and I have found that the default placement of the 'throws' declaration is often confusing, especially to those of us switching from languages where the type of errors thrown is explicitly defined (like Java)

For example,
// This is pretty clear, this can throw an error
func foo() throws
{ ... }

// Also pretty clear, this returns a String
func bar() -> String
{ ... }

// Confusing. Does this throw a String? Does it return a String? Does it do both?
// I personally keep reading this as 'this can throw a String'
func baz() throws -> String

// Equivalent code in Java (not a model, just for clarification of why the above is confusing)
String baz() throws StringFormatException
I therefore suggest either tweaking the syntax around, or moving, the `throws` keyword to avoid this confusion.

Some ideas I've had:
// Add a comma to separate them
func baz() throws, -> String

// Move `throws` to the end
func baz() -> String throws

// Change it to a prefix modifier (like `mutating`)
throwing func baz() -> String
I'm still not sold on any of the above syntaxes, but I would love to hear your feedback.

This would affect existing code, but it would be a fairly small change that would result in very large readability improvements, especially for newcomers, and especially for those coming for a language such as Java.

-thislooksfun (tlf)

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20161226/15a6c7a7/attachment.html>

More information about the swift-evolution mailing list