<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div><br></div><div>On Dec 26, 2016, at 09:38, thislooksfun via swift-evolution <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>> wrote:<br><br></div><blockquote type="cite"><div><meta http-equiv="Content-Type" content="text/html charset=us-ascii">Hello Swifters,<div class=""><br class=""></div><div class="">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)</div><div class=""><br class=""></div><div class="">For example,</div><div class=""><div class="bloop_markdown" style="font-family: Helvetica, Arial; font-size: 13px; background-color: rgb(254, 254, 254);"><pre style="margin-top: 15px; margin-bottom: 15px; font-family: Menlo, Consolas, 'Liberation Mono', Courier, monospace; font-size: 10pt; border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; background-color: rgb(248, 248, 248); color: inherit; border: 1px solid rgb(204, 204, 204); overflow: auto; padding: 4px 8px; word-break: normal; word-wrap: normal;" class="">// 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</pre></div><div class="">I therefore suggest either tweaking the syntax around, or moving, the `throws` keyword to avoid this confusion.</div><div class=""><br class=""></div><div class="">Some ideas I've had:</div><div class=""><pre style="color: inherit; margin-top: 15px; margin-bottom: 15px; font-family: Menlo, Consolas, 'Liberation Mono', Courier, monospace; font-size: 10pt; border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; background-color: rgb(248, 248, 248); border: 1px solid rgb(204, 204, 204); overflow: auto; padding: 4px 8px; word-break: normal; word-wrap: normal;" class="">// 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
</pre></div><div class="">I'm still not sold on any of the above syntaxes, but I would love to hear your feedback.</div><div class=""><br class=""></div><div class="">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 <i class="">especially</i> for those coming for a language such as Java.</div><div class=""><br class=""></div><div class=""><div style="color: rgb(0, 0, 0); letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">-thislooksfun (tlf)</div></div></div></div></blockquote><br><div>Does `throws` affect the actual return type? That is, is the size of the returned data different between "func foo() -> Int8" and "func foo() throws -> Int8"? If so, the "throws" is quite literally part of the return type and the current syntax reflects that. If not, I <i>think</i> I'd probably be in favor of that last "prefix modifier" suggestion with either "throwing" or "@throwing" (depending on the exact semantics of the "@" part — I'm a bit unclear on that detail)... probably... maybe... I'll have to think about it some more.</div><div><br></div><div>- Dave Sweeris</div><div><br></div></body></html>