<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class="">+1<div class=""><br class=""></div><div class="">It would be nice to have&nbsp;<i class="">generic</i>&nbsp;supertype constraints, with syntax along the lines of `where A:B`.&nbsp;</div><div class=""><br class=""></div><div class="">Not sure if this is the same as what’s being suggested.</div><div class=""><br class=""></div><div class="">Example:</div><div class=""><pre class="">struct ViewWrapper&lt;T:UIView&gt; {
    let views:[T]
    func add&lt;V:UIView&gt;(_ view:V) -&gt; ViewWrapper&lt;T&gt; where V:T { // V must be a subtype of T
        let newViews = views + [view] // pseudo code
        return ViewWrapper(views: newViews)
    }
}
let controls  = ViewWrapper&lt;UIControl&gt;(views:[])
controls.add(UIButton()) // succeeds, UIButton is a UIControl
controls.add(UIView()) // fails, UIView is not a UIControl</pre><div class="">David</div><div><br class=""><blockquote type="cite" class=""><div class="">On 6 Dec 2017, at 00:34, Rex Fenley 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 dir="ltr" class="">Huge +1, I've asked for this in the past too.<div class=""><br class=""></div><div class="">Have you also found this limitation frustrating?&nbsp;</div><div class="">&nbsp; - Yes</div><div class=""><br class=""></div><div class="">In what contexts?</div><div class="">&nbsp; - APIs that have this requirement and end up enforcing them through runtime type checking and throws. Shows up in some network data mapping code I have that generalizes over Core Data and Realm (and other databases). The protocol implementer must specify the subtype for the raw mapping of JSON and base type for the DB reading/writing layer. Could see this showing up whenever there's a separation of concerns between what business logic belongs to the base type and subtypes of a more generalized system. I could potentially see the same issue showing up in code generalizing the mapping of data to UI, like UITableView/UITableViewCell.</div><div class=""><br class=""></div><div class="">Does anyone have reservations about introducing this capability?<br class=""></div><div class="">&nbsp; - I do not</div><div class=""><div class=""><blockquote type="cite" class="gmail-m_2306242412896594494clean_bq" style="font-size:12.8px"><div style="word-wrap:break-word" class="">One of the most frequent frustrations I encounter when writing generic code in Swift is the requirement that supertype constraints be concrete.&nbsp; When I mentioned this on Twitter (<a href="https://twitter.com/anandabits/status/929958479598534656" target="_blank" class="">https://twitter.com/<wbr class="">anandabits/status/<wbr class="">929958479598534656</a>) Doug Gregor mentioned that this feature is smaller and mostly straightforward to design and implement (<a href="https://twitter.com/dgregor79/status/929975472779288576" target="_blank" class="">https://twitter.com/<wbr class="">dgregor79/status/<wbr class="">929975472779288576</a>).<div class=""><div class=""><br class=""></div><div class="">I currently have a PR open to add the high-level description of this feature found below to the generics manifesto (<a href="https://github.com/apple/swift/pull/13012" target="_blank" class="">https://github.com/apple/<wbr class="">swift/pull/13012</a>):</div><div class=""><br class=""></div><div class=""><div class="">Currently, supertype constraints may only be specified using a concrete class or protocol type.&nbsp; This prevents us from abstracting over the supertype.</div><div class=""><br class=""></div><div class="">```swift</div><div class="">protocol P {</div><div class="">&nbsp; associatedtype Base</div><div class="">&nbsp; associatedtype Derived: Base</div><div class="">}</div><div class="">```</div><div class=""><br class=""></div><div class="">In the above example `Base` may be any type. &nbsp;`Derived` may be the same as `Base` or may be _any_ subtype of `Base`.&nbsp; All subtype relationships supported by Swift should be supported in this context including, but not limited to, classes and subclasses, existentials and conforming concrete types or refining existentials, `T?` and &nbsp;`T`, `((Base) -&gt; Void)` and `((Derived) -&gt; Void)`, etc.</div><div class=""><br class=""></div><div class="">Generalized supertype constraints would be accepted in all syntactic locations where generic constraints are accepted.</div></div><div class=""><br class=""></div><div class="">I would like to see generalized supertype constraints make it into Swift 5 if possible.&nbsp; I am not an implementer so I will not be able to bring a proposal forward alone but am interested in collaborating with anyone interested in working on implementation.</div><div class=""><br class=""></div><div class="">I am also interested in hearing general feedback on this feature from the community at large.&nbsp; Have you also found this limitation frustrating?&nbsp; In what contexts?&nbsp; Does anyone have reservations about introducing this capability?&nbsp; If so, what are they?<div class=""><br class=""></div><div class=""><div class="">Matthew</div><div class=""><br class=""></div></div></div></div>______________________________<wbr class="">_________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank" class="">https://lists.swift.org/<wbr class="">mailman/listinfo/swift-<wbr class="">evolution</a></div></blockquote><div class=""><br class=""></div>-- <br class=""><div class="gmail_signature"><div dir="ltr" class=""><span class=""><div style="line-height: 1.15; margin-top: 0pt; margin-bottom: 0pt;" class=""><span style="font-size:16px;font-family:Arial;background-color:transparent;font-style:italic;vertical-align:baseline;white-space:pre-wrap" class="">Rex Fenley</span><span style="font-size:16px;font-family:Arial;background-color:transparent;vertical-align:baseline;white-space:pre-wrap" class=""> &nbsp;</span><span style="font-size:11px;font-family:Arial;color:rgb(153,153,153);background-color:transparent;vertical-align:baseline;white-space:pre-wrap" class="">|</span><span style="line-height:1.15;font-family:Arial;color:rgb(153,153,153);background-color:transparent;vertical-align:baseline;white-space:pre-wrap" class=""> &nbsp;</span><span style="font-size:11px;font-family:Arial;color:rgb(153,153,153);background-color:transparent;vertical-align:baseline;white-space:pre-wrap" class="">IOS DEVELOPER</span><br class=""></div></span><span class=""><br class=""><div style="line-height: 1.15; margin-top: 0pt; margin-bottom: 0pt;" class=""><span style="font-size:11px;font-family:Arial;color:rgb(153,153,153);vertical-align:baseline;white-space:pre-wrap;background-color:transparent" class=""><img src="https://lh5.googleusercontent.com/xMgzw3JkFL3DLkdwyq0WxJzKs_XP57gVVCaBMvgi1FKCjSeue0xdx3JZeCWBlxN4KRHhHOfdvJbc1N-AjTwXcKIq4cjJg9H7iaFpQ8WbO4N3c9Y5dzi19cPOs_owPquuqw" width="250px;" height="53px;" style="border: none;" class=""></span></div><div style="line-height: 1.15; margin-top: 0pt; margin-bottom: 0pt;" class=""><a href="https://www.remind.com/" style="text-decoration:none" target="_blank" class=""><span style="font-size:11px;font-family:Arial;color:rgb(17,85,204);font-weight:bold;text-decoration:underline;vertical-align:baseline;white-space:pre-wrap;background-color:transparent" class="">Remind.com</span></a><span style="font-family:Arial;vertical-align:baseline;white-space:pre-wrap;background-color:transparent" class=""> </span><span style="font-size:11px;font-family:Arial;vertical-align:baseline;white-space:pre-wrap;background-color:transparent" class="">| &nbsp;</span><a href="http://blog.remind.com/" style="text-decoration:none" target="_blank" class=""><span style="font-size:11px;font-family:Arial;color:rgb(17,85,204);text-decoration:underline;vertical-align:baseline;white-space:pre-wrap;background-color:transparent" class="">BLOG</span></a><span style="font-size:11px;font-family:Arial;vertical-align:baseline;white-space:pre-wrap;background-color:transparent" class=""> &nbsp;| &nbsp;</span><a href="https://twitter.com/remindhq" style="text-decoration:none" target="_blank" class=""><span style="font-size:11px;font-family:Arial;color:rgb(17,85,204);text-decoration:underline;vertical-align:baseline;white-space:pre-wrap;background-color:transparent" class="">FOLLOW US</span></a><span style="font-size:11px;font-family:Arial;vertical-align:baseline;white-space:pre-wrap;background-color:transparent" class=""> &nbsp;| </span><span style="font-family:Arial;vertical-align:baseline;white-space:pre-wrap;background-color:transparent" class="">&nbsp;</span><span style="text-decoration:underline;font-size:11px;font-family:Arial;color:rgb(17,85,204);vertical-align:baseline;white-space:pre-wrap;background-color:transparent" class=""><a href="https://www.facebook.com/remindhq" style="text-decoration:none" target="_blank" class="">LIKE US</a></span></div></span></div></div>
</div></div></div>
_______________________________________________<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=""></div></blockquote></div><br class=""><div class="">
<div style="color: rgb(0, 0, 0); font-family: Verdana;  font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div style="color: rgb(0, 0, 0); font-family: Verdana; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><span class="Apple-style-span" style="border-collapse: separate; font-variant-ligatures: normal; font-variant-east-asian: normal; font-variant-position: normal; line-height: normal; border-spacing: 0px;"><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Verdana; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; border-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-stroke-width: 0px;"><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Verdana; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; border-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-stroke-width: 0px;"><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">David James</div></div></span></div></span></div></span></div></div>
</div>
<br class=""></div></body></html>