<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="">This kind of sounds like the proposal to have implicit return in guard statements.<div class=""><br class=""></div><div class="">I agree with John that this kind of feels incosistent and obscure.</div><div class=""><br class=""></div><div class="">An alternative that could be used everywhere though may look like this:</div><div class=""><br class=""></div><div class="">func myFunc() -&gt; String {</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>autoreturn {</div><div class=""><span class="Apple-tab-span" style="white-space:pre">                </span>return "Hello"</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>}</div><div class=""><br class=""></div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>if Date.today.isSunday {</div><div class=""><span class="Apple-tab-span" style="white-space:pre">                </span>return "Sunday"</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>}</div><div class="">}</div><div class=""><br class=""></div><div class="">The `autoreturn` would act as `defer` on paths that do not have a return statement, would be applicable in any function (i.e. any return type allowed).</div><div class=""><br class=""></div><div class="">I can see this being useful if you need to return default value that needs to have some further initialization and the method is quite long.<br class=""><div class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Jun 22, 2016, at 10:55 PM, John McCall via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div style="font-family: Helvetica; font-size: 10px; font-style: normal; font-variant-caps: normal; font-weight: normal; 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;" class=""><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 class=""><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 style="font-family: Helvetica; font-size: 10px; font-style: normal; font-variant-caps: normal; font-weight: normal; 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;" class=""><br class=""></div><div style="font-family: Helvetica; font-size: 10px; font-style: normal; font-variant-caps: normal; font-weight: normal; 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;" class=""><div class="" style="margin: 0px; line-height: normal; font-family: 'Andale Mono'; color: rgb(41, 249, 20); background-color: rgb(0, 0, 0);"><span class="" style="font-variant-ligatures: no-common-ligatures;">(swift) func foo() -&gt; ()? {}</span></div><div class="" style="margin: 0px; line-height: normal; font-family: 'Andale Mono'; color: rgb(0, 249, 0); background-color: rgb(0, 0, 0);"><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(52, 187, 199);">&lt;REPL Input&gt;:1:20:<span class="Apple-converted-space">&nbsp;</span></span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(195, 55, 32);">error:<span class="Apple-converted-space">&nbsp;</span></span><span class="" style="font-variant-ligatures: no-common-ligatures;">missing return in a function expected to return '()?'</span></div><div class="" style="margin: 0px; line-height: normal; font-family: 'Andale Mono'; color: rgb(41, 249, 20); background-color: rgb(0, 0, 0);"><span class="" style="font-variant-ligatures: no-common-ligatures;">func foo() -&gt; ()? {}</span></div><div class="" style="margin: 0px; line-height: normal; font-family: 'Andale Mono'; color: rgb(52, 189, 38); background-color: rgb(0, 0, 0);"><span class="" style="font-variant-ligatures: no-common-ligatures;">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ^</span></div><div class=""><span class="" style="font-variant-ligatures: no-common-ligatures;"><br class=""></span></div><div class=""><span class="" style="font-variant-ligatures: no-common-ligatures;">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 class="" style="font-variant-ligatures: no-common-ligatures;"><br class=""></span></div><div class=""><span class="" style="font-variant-ligatures: no-common-ligatures;">John.</span></div></div><div style="font-family: Helvetica; font-size: 10px; font-style: normal; font-variant-caps: normal; font-weight: normal; 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;" class=""><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 ------------<span class="Apple-converted-space">&nbsp;</span><br class="">Group: gmane.comp.lang.swift.evolution<span class="Apple-converted-space">&nbsp;</span><br class="">MsgID: &lt;<a href="mailto:4AC6F31E-9E46-47B3-8CAE-B5EDD04043D5@gmail.com" class="">4AC6F31E-9E46-47B3-8CAE-B5EDD04043D5@gmail.com</a>&gt;<span class="Apple-converted-space">&nbsp;</span><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<span class="Apple-converted-space">&nbsp;</span><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<span class="Apple-converted-space">&nbsp;</span><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 {}<span class="Apple-converted-space">&nbsp;</span><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=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class=""><br class=""><br class="">------------- End Message -------------<span class="Apple-converted-space">&nbsp;</span><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="" style="font-family: Helvetica; font-size: 10px; font-style: normal; font-variant-caps: normal; font-weight: normal; 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;"><span style="font-family: Helvetica; font-size: 10px; font-style: normal; font-variant-caps: normal; font-weight: normal; 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; float: none; display: inline !important;" class="">_______________________________________________</span><br style="font-family: Helvetica; font-size: 10px; font-style: normal; font-variant-caps: normal; font-weight: normal; 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;" class=""><span style="font-family: Helvetica; font-size: 10px; font-style: normal; font-variant-caps: normal; font-weight: normal; 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; float: none; display: inline !important;" class="">swift-evolution mailing list</span><br style="font-family: Helvetica; font-size: 10px; font-style: normal; font-variant-caps: normal; font-weight: normal; 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;" class=""><a href="mailto:swift-evolution@swift.org" style="font-family: Helvetica; font-size: 10px; font-style: normal; font-variant-caps: normal; font-weight: normal; 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;" class="">swift-evolution@swift.org</a><br style="font-family: Helvetica; font-size: 10px; font-style: normal; font-variant-caps: normal; font-weight: normal; 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;" class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" style="font-family: Helvetica; font-size: 10px; font-style: normal; font-variant-caps: normal; font-weight: normal; 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;" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a></div></blockquote></div><br class=""></div></div></body></html>