<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 2016-01-20, at 11:25, zhaoxin肇鑫 &lt;<a href="mailto:owenzx@gmail.com" class="">owenzx@gmail.com</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class=""><div class="gmail_default" style="font-family:georgia,serif">What about dropping T, just using Element, and converting Element to the type in your predicate?</div></div>
</div></blockquote></div><br class=""><div class="">As I wrote in my original message, I’m writing a whole set of&nbsp;<span style="font-family: Menlo; font-size: 11px;" class="">findFirst()</span>&nbsp;methods and one of them simply takes a predicate and returns an&nbsp;<span style="color: rgb(112, 61, 170); font-family: Menlo; font-size: 11px;" class="">Element</span>:</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(187, 44, 162);" class="">@warn_unused_result</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">func</span> findFirst(<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">@noescape</span> predicate: <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">Self</span>.Generator.Element <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">throws</span> -&gt; <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Bool</span>) <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">rethrows</span> -&gt; <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Self</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Generator</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Element</span>? {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp;&nbsp;<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">for</span> element <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">in</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">self</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">where</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">try</span> predicate(element) {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span> element</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp;&nbsp;}</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp;&nbsp;<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">nil</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">}</div></div><div class=""><br class=""></div><div class="">If I understand correctly, you propose to check the type of the passed-in&nbsp;<span style="color: rgb(112, 61, 170); font-family: Menlo; font-size: 11px;" class="">Element</span>&nbsp;in the predicate, like this:</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">let</span> result1 = <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">containerView</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">subviews</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">findFirst</span> { ($0 <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">as</span>? <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">NSButton</span>)?.<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">state</span> == <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">NSOnState</span> }</div></div><div class=""><br class=""></div><div class="">But that is cumbersome and has a very significant drawback: The type of&nbsp;<span style="font-family: Menlo; font-size: 11px;" class="">result1</span>&nbsp;is now&nbsp;<span style="color: rgb(112, 61, 170); font-family: Menlo; font-size: 11px;" class="">NSView</span>, not&nbsp;<span style="color: rgb(112, 61, 170); font-family: Menlo; font-size: 11px;" class="">NSButton</span>.</div><div class="">That’s the whole point for the generic version of the method in my original message: the predicate doesn’t have to check the type, and the result of&nbsp;<span style="font-family: Menlo; font-size: 11px;" class="">findFirst()</span>&nbsp;has the type the caller specifies.</div><div class="">So to get the same result as with my generic method (but uglier and slower due to more type casts) you’d have to do this:</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">let</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> result2 = </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">containerView</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">.</span>subviews<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">findFirst</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> { ($0 </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">as</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">? </span>NSButton<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">)?.</span>state<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> == </span>NSOnState<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> } </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">as</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">? </span>NSButton</div></div><div class=""><br class=""></div><div class="">I think that’s not very nice compared to how the two generic versions in question look:</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">let</span> result3 = <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">containerView</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">subviews</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">findFirst</span>(<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">NSButton</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">self</span>, matching: { $0.<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">state</span> == <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">NSOnState</span> })</div></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-family: Helvetica; font-size: 12px;" class="">or:</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="color: rgb(187, 44, 162);" class="">let</span> result4: <span style="color: rgb(112, 61, 170);" class="">NSButton</span>? = <span style="color: rgb(79, 129, 135);" class="">containerView</span>.<span style="color: rgb(112, 61, 170);" class="">subviews</span>.<span style="color: rgb(49, 89, 93);" class="">findFirst</span>&nbsp;{ $0.<span style="color: rgb(112, 61, 170);" class="">state</span> == <span style="color: rgb(112, 61, 170);" class="">NSOnState</span> }</div></body></html>