<html><head><style>body{font-family:Helvetica,Arial;font-size:13px}</style></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><div id="bloop_customfont" style="font-family:Helvetica,Arial;font-size:13px; color: rgba(0,0,0,1.0); margin: 0px; line-height: auto;">Such something went wrong with the `[Stage-2]` annotation? </div> <br> <div id="bloop_sign_1487319782191840000" class="bloop_sign"><div style="font-family:helvetica,arial;font-size:13px">-- <br>Adrian Zubarev<br>Sent with Airmail</div></div> <br><p class="airmail_on">Am 17. Februar 2017 um 09:20:41, Adrian Zubarev (<a href="mailto:adrian.zubarev@devandartist.com">adrian.zubarev@devandartist.com</a>) schrieb:</p> <blockquote type="cite" class="clean_bq"><span><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><div></div><div>
<title></title>
<div class="bloop_markdown">
<p>I’d like to revive an additive proposal that couldn’t make it
into Swift 3. This proposal has a small improvement to the language
compared to other big features currently being proposed. It almost
feels like a bug fix rather than a new feature, but it still needs
a full and quick review process.</p>
<p>You can read the formatted version here: <a href="https://github.com/apple/swift-evolution/pull/608">https://github.com/apple/swift-evolution/pull/608</a></p>
<h1 id="returnconsistencyforsingle-expressions"><code>return</code>
consistency for single-expressions</h1>
<ul>
<li>Proposal: <a href="https://github.com/apple/swift-evolution/blob/master/proposals/nnnn-single-expression-optional-return.md">
SE-NNNN</a></li>
<li>Author: <a href="https://github.com/DevAndArtist">Adrian
Zubarev</a></li>
<li>Status: <strong><a href="#rationale">Awaiting
review</a></strong></li>
<li>Review manager: TBD</li>
</ul>
<h2 id="introduction">Introduction</h2>
<p>Any single-expression closure can omit the <code>return</code>
statement. This proposal aims to make this feature more consistent
in some other corners of the language.</p>
<p>Original swift-evolution thread: * <a href="">[Pitch] [Stage–2]
<code>return</code> consistency for single-expressions</a> *
<a href="https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160523/019260.html">
[Pitch] (Bofore Swift 3) Make <code>return</code> optional in
computed properties for a single case</a></p>
<h2 id="motivation">Motivation</h2>
<p>Closures can omit the <code>return</code> and have an inferred
return type:</p>
<pre><code class="swift">let _ = { 42 } // Type: () -> Int
let _ = [1,2,3].map { $0 * 5 } // T == Int
</code></pre>
<p>There are also value returning code blocks in the language that
feel the same but are inconsistent to the mentioned feature:</p>
<pre><code class="swift">// Read-write computed property:
var integer: Int {
get { return 2016 }
set { /* do some work */ }
}
// Read-only computed property:
var string: String { return "hello swift" }
// Function:
func pi() -> Double {
return 3.141
}
// Read-Write subscript:
subscript(index: Int) -> Int {
get { return index % 2 }
set { /* do some work */ }
}
// Read-only subscript:
subscript(index: Int) -> Int { return index * 2 }
</code></pre>
<h2 id="proposedsolution">Proposed solution</h2>
<p>Make <code>return</code> optional for the following top level
code blocks that only contain a single expression:</p>
<ul>
<li><em>variable-declaration</em></li>
<li><em>getter-setter-block</em></li>
<li><em>getter-clause</em></li>
<li><em>function-body</em></li>
<li><em>subscript-declaration</em></li>
</ul>
<p>That will allow us to rewrite the above example to:</p>
<pre><code class="swift">// Read-Write computed property:
var integer: Int {
get { 2016 }
...
}
// Read-only computed property:
var string: String { "hello swift" }
// Function:
func pi() -> Double { 3.141 }
// Read-Write subscript:
subscript(index: Int) -> Int {
get { index % 2 }
...
}
// Read-only subscript:
subscript(index: Int) -> Int { index * 2 }
</code></pre>
<p><strong>Possible real world example:</strong></p>
<pre><code class="swift">// Today
public struct Character {
public let source: Module.Source
private let _pointer: UnsafePointer<Swift.Character>
public var value: Swift.Character {
return self._pointer.pointee
}
...
}
// Rewritten:
public struct Character {
...
public var value: Swift.Character { self._pointer.pointee }
...
}
</code></pre>
<h2 id="impactonexistingcode">Impact on existing code</h2>
<p>None, this change will only relax some existing rules.</p>
<h2 id="alternativesconsidered">Alternatives considered</h2>
<p>Leave this as is and live with such inconsistency.</p>
</div>
<div class="bloop_original_html">
<div id="bloop_customfont" style="font-family:Helvetica,Arial;font-size:13px; color: rgba(0,0,0,1.0); margin: 0px; line-height: auto;">
<br></div>
<br>
<div id="bloop_sign_1487319131171746048" class="bloop_sign">
<div style="font-family:helvetica,arial;font-size:13px">
-- <br>
Adrian Zubarev<br>
Sent with Airmail</div>
</div>
</div>
<div class="bloop_markdown"></div>
</div></div></span></blockquote></body></html>