<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div><blockquote type="cite" class=""><div class="">On Jun 22, 2016, at 1:36 PM, James Froggatt via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:</div><div class=""><div class="">Welcome, Logan.<br class=""><br class="">Functions currently return the empty tuple, ‘()’, by default. Void is a typealias to the empty tuple type. It is also possible to write ‘return ()’ explicitly, rather than just ‘return’. (This is generally a detail of the language, so may be unfamiliar)<br class=""></div></div></blockquote><div><br class=""></div>It is more correct to say that it is illegal to reach the end of a function whose return type is not (). &nbsp;Falling off the end of a function that returns ()? will not implicitly return Optional.Some(()); it will produce an error:</div><div><br class=""></div><div><div style="margin: 0px; line-height: normal; font-family: 'Andale Mono'; color: rgb(41, 249, 20); background-color: rgb(0, 0, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">(swift) func foo() -&gt; ()? {}</span></div><div style="margin: 0px; line-height: normal; font-family: 'Andale Mono'; color: rgb(0, 249, 0); background-color: rgb(0, 0, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #34bbc7" class="">&lt;REPL Input&gt;:1:20: </span><span style="font-variant-ligatures: no-common-ligatures; color: #c33720" class="">error: </span><span style="font-variant-ligatures: no-common-ligatures" class="">missing return in a function expected to return '()?'</span></div><div style="margin: 0px; line-height: normal; font-family: 'Andale Mono'; color: rgb(41, 249, 20); background-color: rgb(0, 0, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">func foo() -&gt; ()? {}</span></div><div style="margin: 0px; line-height: normal; font-family: 'Andale Mono'; color: rgb(52, 189, 38); background-color: rgb(0, 0, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ^</span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><br class=""></span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class="">We could amend this rule to permit reaching the end of an optional-returning function, with the semantics of returning nil. &nbsp;I do not, however, think that would be a good idea; it turns simple mistakes into bugs, feels inconsistent in the language, and is unnecessarily obscure for readers.</span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><br class=""></span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class="">John.</span></div></div><div><br class=""><blockquote type="cite" class=""><div class=""><div class=""><br class="">It is unclear in your proposal as it stands which (if any) a function returning ‘()?’ would use as its default: (), or nil?<br class="">Would the ‘nil’ keyword still be required when writing return explicitly?<br class=""><br class="">While this does match behaviour present in other parts of the language, and I see the benefit of having implicit returns in this otherwise straightforward case, I'm struggling to decide as to whether this is worth the extra complexity of having two orthogonal implicit return mechanisms.<br class=""><br class="">------------ Begin Message ------------ <br class="">Group: gmane.comp.lang.swift.evolution <br class="">MsgID: &lt;<a href="mailto:4AC6F31E-9E46-47B3-8CAE-B5EDD04043D5@gmail.com" class="">4AC6F31E-9E46-47B3-8CAE-B5EDD04043D5@gmail.com</a>&gt; <br class=""><br class="">I believe Swift should no longer require an explicit return on all functions and instead do an implicit nil return if the function reaches the end of its control flow and has an optional return type.<br class=""><br class="">This could be useful to keep code clean and compact, by only having to write code for cases that our function handles and just returning nil otherwise automatically.<br class=""><br class=""><br class="">Consider:<br class=""><br class="">func toInt(string : String?) -&gt; Int?<br class="">{<br class="">if let s = string<br class="">{<br class="">return s.intValue<br class="">}<br class=""><br class="">//Make this return implicitly happen instead of requiring it.<br class="">//return nil <br class="">}<br class=""><br class=""><br class=""><br class="">This also very much goes along with the implicit return within a guard statement that I have seen proposed. Here:<br class=""><br class="">func toInt(string: String?) -&gt; Int?<br class="">{<br class="">guard let s = string else {<br class="">//this could be implicitly returned using the same logic, since the guard means we have reached the end of our code path without returning<br class="">//return nil <br class="">}<br class="">return s.toInt()<br class="">}<br class=""><br class=""><br class="">These methods could be re-written as so:<br class=""><br class="">This could allow us to write the examples below much cleaner<br class="">func toInt(string : String?) -&gt; Int?<br class="">{<br class="">if let s = string<br class="">{<br class="">return s.toInt()<br class="">}<br class="">}<br class=""><br class="">func toInt(string: String?) -&gt; Int?<br class="">{<br class="">guard let s = string else {} <br class="">return s.toInt()<br class="">}<br class=""><br class="">// it would be even cooler if we could omit the else {} and make them not it return by default. But that’s another thing all together<br class="">func toInt(string: String?) -&gt; Int?<br class="">{<br class="">guard let s = string<br class="">return s.toInt()<br class="">}<br class=""><br class=""><br class="">Thanks for reading my first post to the Swift open source discussion board!<br class="">-Logan<br class=""><br class=""><br class="">_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""><br class=""><br class="">------------- End Message ------------- <br class=""><br class=""><br class=""><br class="">From James F<br class="">_______________________________________________<br class="">swift-evolution mailing list<br class="">swift-evolution@swift.org<br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></div></blockquote></div><br class=""></body></html>