<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=""><br class=""><div><blockquote type="cite" class=""><div class="">On Feb 23, 2017, at 10:54 AM, Adrian Zubarev via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div class="bloop_markdown" style="font-family: Helvetica, Arial; font-size: 13px; 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; background-color: rgb(254, 254, 254);"><p style="margin: 15px 0px; -webkit-margin-before: 0px;" class="">First of all, writing CRTP is already allowed in Swift, however it doesn’t work at all.<span class="Apple-converted-space"> </span></p><p style="margin: 15px 0px;" class="">For example one would assume that the output of the following code sample might look like this<span class="Apple-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; -webkit-margin-before: 0px;" class="">1 2 3 4</code>:</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;" class=""><code class="swift" 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; -webkit-margin-before: 0px;">print(1)
class Template<T> {}
class RealThing : Template<RealThing> {
override init() { print(2) }
func foo() { print(3) }
}
let instance = RealThing()
instance.foo()
print(4)
</code></pre><p style="margin: 15px 0px;" class="">However the result is only<span class="Apple-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; -webkit-margin-before: 0px;" class="">1</code><span class="Apple-converted-space"> </span>and your application will simply stuck at that point without crashing or throwing any bad runtime error.</p><p style="margin: 15px 0px;" class=""><a href="https://bugs.swift.org/browse/SR-2549" style="color: rgb(65, 131, 196); background-color: inherit; text-decoration: none; -webkit-margin-before: 0px;" class="">https://bugs.swift.org/browse/SR–2549</a></p><p style="margin: 15px 0px;" class="">I discovered an API in UIKit which uses that pattern, obviously it’s written in Objective-C but it is imported in Swift and it actually works, because it’s not pure swift.</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;" class=""><code class="swift" 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; -webkit-margin-before: 0px;">// Generic superclass
open class NSLayoutAnchor<AnchorType : AnyObject> : NSObject { ... }
// Non-generic subclasses with the `Sub : Super<Sub>` pattern
open class NSLayoutXAxisAnchor : NSLayoutAnchor<NSLayoutXAxisAnchor> {}
open class NSLayoutYAxisAnchor : NSLayoutAnchor<NSLayoutYAxisAnchor> {}
open class NSLayoutDimension : NSLayoutAnchor<NSLayoutDimension> { ... }
</code></pre><p style="margin: 15px 0px;" class="">My questions are:</p><ul style="margin: 15px 0px;" class=""><li style="margin: 15px 0px; -webkit-margin-before: 0px;" class="">Will we ever allow this in Swift?</li><li style="margin: 15px 0px;" class="">Does CRTP bring any technical improvement to Swifts generic system?</li><li style="margin: 15px 0px;" class="">Is it something for Swift 4 to consider?</li><li style="margin: 15px 0px;" class="">Does it need a proposal or should we only see it as a bug?</li></ul></div></div></blockquote><br class=""></div><div>For those who might not know what the acronym stands for: <a href="https://en.wikipedia.org/wiki/Curiously_recurring_template_pattern" class="">https://en.wikipedia.org/wiki/Curiously_recurring_template_pattern</a></div><div><br class=""></div><div>I can’t think of any non-technical reasons to prohibit it, so to me, this not being supported is either a bug or a technical limitation of type system. Actually, I’d say the specific behavior you’re describing is a bug regardless, since the compiler should throw an error if it comes across code that it can see will get the type system into an infinite loop.</div><div><br class=""></div><div>- Dave Sweeris</div></body></html>