<div dir="ltr">`Self` _always_ refers to the dynamic type of `self`. It just happens to be that in the case of structs the dynamic type is the same as the static type. The idea of having a shorthand for the containing type (spelled #Self or StaticSelf) was discussed during consideration of SE-0068. The accepted version of the proposal rejects that idea, having adopted the position that "You will continue to specify full type names for any other use. Joe Groff writes, 'I don't think it's all that onerous to have to write ClassName.foo if that's really what you specifically mean.'"<div class="gmail_extra"><br></div><div class="gmail_extra"><br><div class="gmail_quote">On Sat, Jan 7, 2017 at 11:14 AM, thislooksfun via swift-evolution <span dir="ltr"><<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div style="word-wrap:break-word">I like this idea, however, if I understand the proposal correctly, I think that the naming would make more sense the other way around. `Self` is, at least in my head, tied directly and statically to the enclosing type, where as `Current` sounds more dynamic, and could change from place-to-place.<br><div>
<div style="color:rgb(0,0,0);letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;word-wrap:break-word"><br>-thislooksfun (tlf)</div>
</div>
<br><div><blockquote type="cite"><div><div class="gmail-h5"><div>On Jan 7, 2017, at 4:38 AM, Adrian Zubarev via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>> wrote:</div><br class="gmail-m_6743338456365996728Apple-interchange-newline"></div></div><div><div><div class="gmail-h5"><div class="gmail-m_6743338456365996728bloop_markdown" style="font-family:helvetica,arial;font-size:13px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;background-color:rgb(254,254,254)"><p style="margin:15px 0px">Hi Swift community,</p><p style="margin:15px 0px">I’d like to talk to about current<span class="gmail-m_6743338456365996728Apple-converted-space"> </span><code style="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(234,234,234);margin:0px 2px;padding:0px 5px;word-break:normal;word-wrap:normal">Self</code><span class="gmail-m_6743338456365996728Apple-converted-space"> </span>keyword. If I’m not totally mistaken then the current<span class="gmail-m_6743338456365996728Apple-converted-space"> </span><code style="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(234,234,234);margin:0px 2px;padding:0px 5px;word-break:normal;word-wrap:normal">Self</code><span class="gmail-m_6743338456365996728Apple-converted-space"> </span>has a few meanings:</p><ol style="margin:15px 0px"><li style="margin:15px 0px">Refer to the<span class="gmail-m_6743338456365996728Apple-converted-space"> </span><em>current</em><span class="gmail-m_6743338456365996728Apple-converted-space"> </span>type, or refer to the dynamic type for non-final classes inside containing type (SE–0068 - not yet implemented).</li><li style="margin:15px 0px">For non-final class types use<span class="gmail-m_6743338456365996728Apple-converted-space"> </span><code style="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(234,234,234);margin:0px 2px;padding:0px 5px;word-break:normal;word-wrap:normal">Self</code><span class="gmail-m_6743338456365996728Apple-converted-space"> </span>as return type on the conforming super type (or<span class="gmail-m_6743338456365996728Apple-converted-space"> </span><em>return an instance of receiver<span class="gmail-m_6743338456365996728Apple-converted-space"> </span><code style="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(234,234,234);margin:0px 2px;padding:0px 5px;word-break:normal;word-wrap:normal">Self</code></em>).</li></ol><p style="margin:15px 0px">Let me visualize the behaviors quickly in some short code snippet:</p><pre style="margin:15px 0px;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"><code class="gmail-m_6743338456365996728swift" style="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:0px;margin:0px;padding:0px;word-break:normal;word-wrap:normal">protocol Foo {
func foo(_ f: Self) -> Self
}
class A : Foo {
// forced to use `A` as parameter type and `Self` as return type
func foo(_ f: A) -> Self { return self }
// Returning `A()` would cause an error: Cannot convert return expression of type 'A' to return type 'Self'
func bar() -> A { return A() /* or self */ }
func zoo() -> Self { return /* only */ self }
}
class B : A {
// Both is fine `B` or `Self` as the return type
// If `B` is used you can return a different instance like `B()`
// `Self` does only allow `self` to be used here
override func foo(_ f: A) -> B { return self }
}
struct D : Foo {
// No `Self` allowed here at all
func foo(_ f: D) -> D { return self /* or D() */ }
}
</code></pre><p style="margin:15px 0px">The behavior of<span class="gmail-m_6743338456365996728Apple-converted-space"> </span><code style="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(234,234,234);margin:0px 2px;padding:0px 5px;word-break:normal;word-wrap:normal">Self</code><span class="gmail-m_6743338456365996728Apple-converted-space"> </span>is a little magical, because it sometimes refers to the<span class="gmail-m_6743338456365996728Apple-converted-space"> </span><em>current</em><span class="gmail-m_6743338456365996728Apple-converted-space"> </span>type it is used in, or it has a contract of using<span class="gmail-m_6743338456365996728Apple-converted-space"> </span><code style="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(234,234,234);margin:0px 2px;padding:0px 5px;word-break:normal;word-wrap:normal">self</code>.</p><hr style="height:0.2em;border:0px;color:rgb(204,204,204);background-color:rgb(204,204,204);display:inherit"><p style="margin:15px 0px">I propose of introducing a new keyword called<span class="gmail-m_6743338456365996728Apple-converted-space"> </span><code style="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(234,234,234);margin:0px 2px;padding:0px 5px;word-break:normal;word-wrap:normal">Current</code><span class="gmail-m_6743338456365996728Apple-converted-space"> </span>to solve a few problems here.</p><ol style="margin:15px 0px"><li style="margin:15px 0px"><p style="margin:15px 0px"><code style="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(234,234,234);margin:0px 2px;padding:0px 5px;word-break:normal;word-wrap:normal">Self</code><span class="gmail-m_6743338456365996728Apple-converted-space"> </span>on parameter types would be disallowed for protocol members, because conformances to that protocol already disallow that (see<span class="gmail-m_6743338456365996728Apple-converted-space"> </span><code style="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(234,234,234);margin:0px 2px;padding:0px 5px;word-break:normal;word-wrap:normal">A</code><span class="gmail-m_6743338456365996728Apple-converted-space"> </span>above). Instead one would use<span class="gmail-m_6743338456365996728Apple-converted-space"> </span><code style="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(234,234,234);margin:0px 2px;padding:0px 5px;word-break:normal;word-wrap:normal">Current</code><span class="gmail-m_6743338456365996728Apple-converted-space"> </span>and get the correct meaning.</p><pre style="margin:15px 0px;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"><code class="gmail-m_6743338456365996728swift" style="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:0px;margin:0px;padding:0px;word-break:normal;word-wrap:normal">protocol Boo {
func boo(_ b: Current) -> Self
}
procotol Loo {
func loo() -> Current
}
class X : Boo, Loo {
func boo(_ b: X) -> Self { return self }
func loo() -> X { return self /* or X() */ }
}
final class Y : Boo {
func boo(_ b: X) -> Y { return self /* or Y */ }
}
</code></pre></li><li style="margin:15px 0px"><p style="margin:15px 0px">Using<span class="gmail-m_6743338456365996728Apple-converted-space"> </span><code style="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(234,234,234);margin:0px 2px;padding:0px 5px;word-break:normal;word-wrap:normal">Self</code><span class="gmail-m_6743338456365996728Apple-converted-space"> </span>inside the containing type would always mean as one would refer to the dynamic type, like the magical syntax function<span class="gmail-m_6743338456365996728Apple-converted-space"> </span><code style="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(234,234,234);margin:0px 2px;padding:0px 5px;word-break:normal;word-wrap:normal">type(of:)</code><span class="gmail-m_6743338456365996728Apple-converted-space"> </span>does.</p></li><li style="margin:15px 0px"><p style="margin:15px 0px"><code style="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(234,234,234);margin:0px 2px;padding:0px 5px;word-break:normal;word-wrap:normal">Current</code><span class="gmail-m_6743338456365996728Apple-converted-space"> </span>can only refer to the current containing type.</p></li><li style="margin:15px 0px"><p style="margin:15px 0px">On classes<span class="gmail-m_6743338456365996728Apple-converted-space"> </span><code style="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(234,234,234);margin:0px 2px;padding:0px 5px;word-break:normal;word-wrap:normal">Self</code><span class="gmail-m_6743338456365996728Apple-converted-space"> </span>has always the contract of returning<span class="gmail-m_6743338456365996728Apple-converted-space"> </span><code style="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(234,234,234);margin:0px 2px;padding:0px 5px;word-break:normal;word-wrap:normal">self</code>.</p></li><li style="margin:15px 0px"><p style="margin:15px 0px"><code style="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(234,234,234);margin:0px 2px;padding:0px 5px;word-break:normal;word-wrap:normal">Self</code><span class="gmail-m_6743338456365996728Apple-converted-space"> </span>could be discouraged in favor of<span class="gmail-m_6743338456365996728Apple-converted-space"> </span><code style="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(234,234,234);margin:0px 2px;padding:0px 5px;word-break:normal;word-wrap:normal">Current</code><span class="gmail-m_6743338456365996728Apple-converted-space"> </span>on value types, as a shorthand to refer to the containing type.</p></li><li style="margin:15px 0px"><p style="margin:15px 0px">Generics could benefit from<span class="gmail-m_6743338456365996728Apple-converted-space"> </span><code style="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(234,234,234);margin:0px 2px;padding:0px 5px;word-break:normal;word-wrap:normal">Current</code><span class="gmail-m_6743338456365996728Apple-converted-space"> </span>too:</p><pre style="margin:15px 0px;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"><code class="gmail-m_6743338456365996728swift" style="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:0px;margin:0px;padding:0px;word-break:normal;word-wrap:normal">extension AReallyLongNonFinalClassName {
func casted<T : Current>() -> T { ... }
}
// `Self` wouldn't here, because it would refer to the dynamic type
</code></pre></li></ol><p style="margin:15px 0px">#1 Would affect a lot of protocols which implies that it would affect ABI.</p><hr style="height:0.2em;border:0px;color:rgb(204,204,204);background-color:rgb(204,204,204);display:inherit"><p style="margin:15px 0px">These are the first ideas I had in my mind. We can polish it further if it receives positive and constructive feedback.</p><p style="margin:15px 0px">Best regards,<span class="gmail-m_6743338456365996728Apple-converted-space"> </span></p><div style="margin:15px 0px"><br class="gmail-m_6743338456365996728webkit-block-placeholder"></div></div><div class="gmail-m_6743338456365996728bloop_original_html" style="font-family:helvetica,arial;font-size:13px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;background-color:rgb(254,254,254)"><div id="gmail-m_6743338456365996728bloop_customfont" style="font-family:helvetica,arial;font-size:13px;margin:0px"><br></div><br><div id="gmail-m_6743338456365996728bloop_sign_1483779578617182976" class="gmail-m_6743338456365996728bloop_sign"><div style="font-family:helvetica,arial;font-size:13px">-- <br>Adrian Zubarev<br>Sent with Airmail</div></div></div><div class="gmail-m_6743338456365996728bloop_markdown" style="font-family:helvetica,arial;font-size:13px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;background-color:rgb(254,254,254)"><div style="margin:15px 0px"><br class="gmail-m_6743338456365996728webkit-block-placeholder"></div></div></div></div><span style="font-family:helvetica,arial;font-size:13px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;background-color:rgb(254,254,254);float:none;display:inline">______________________________<wbr>_________________</span><br style="font-family:helvetica,arial;font-size:13px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;background-color:rgb(254,254,254)"><span style="font-family:helvetica,arial;font-size:13px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;background-color:rgb(254,254,254);float:none;display:inline">swift-evolution mailing list</span><br style="font-family:helvetica,arial;font-size:13px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;background-color:rgb(254,254,254)"><a href="mailto:swift-evolution@swift.org" style="color:rgb(65,131,196);background-color:rgb(254,254,254);text-decoration:none;font-family:helvetica,arial;font-size:13px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px" target="_blank">swift-evolution@swift.org</a><br style="font-family:helvetica,arial;font-size:13px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;background-color:rgb(254,254,254)"><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" style="color:rgb(65,131,196);background-color:rgb(254,254,254);text-decoration:none;font-family:helvetica,arial;font-size:13px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-<wbr>evolution</a><br style="font-family:helvetica,arial;font-size:13px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;background-color:rgb(254,254,254)"></div></blockquote></div><br></div><br>______________________________<wbr>_________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-<wbr>evolution</a><br>
<br></blockquote></div><br></div></div>