<div dir="ltr"><div class="gmail_default" style="font-family:georgia,serif">Then I think the function with NSButton.self is the best. It makes least efforts for a programmer to get no mistakes.</div><div class="gmail_default" style="font-family:georgia,serif"><br></div><div class="gmail_default" style="font-family:georgia,serif">zhaoxin</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Jan 20, 2016 at 7:25 PM, Marco Masser <span dir="ltr"><<a href="mailto:lists@duckcode.com" target="_blank">lists@duckcode.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><span class=""><br><div><blockquote type="cite"><div>On 2016-01-20, at 11:25, zhaoxin肇鑫 <<a href="mailto:owenzx@gmail.com" target="_blank">owenzx@gmail.com</a>> wrote:</div><br><div><div dir="ltr"><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></span><div>As I wrote in my original message, I’m writing a whole set of <span style="font-family:Menlo;font-size:11px">findFirst()</span> methods and one of them simply takes a predicate and returns an <span style="color:rgb(112,61,170);font-family:Menlo;font-size:11px">Element</span>:</div><div><br></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(187,44,162)">@warn_unused_result</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:#bb2ca2">func</span> findFirst(<span style="color:#bb2ca2">@noescape</span> predicate: <span style="color:#bb2ca2">Self</span>.Generator.Element <span style="color:#bb2ca2">throws</span> -> <span style="color:#703daa">Bool</span>) <span style="color:#bb2ca2">rethrows</span> -> <span style="color:#703daa">Self</span>.<span style="color:#703daa">Generator</span>.<span style="color:#703daa">Element</span>? {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:#bb2ca2">for</span> element <span style="color:#bb2ca2">in</span> <span style="color:#bb2ca2">self</span> <span style="color:#bb2ca2">where</span> <span style="color:#bb2ca2">try</span> predicate(element) {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:#bb2ca2">return</span> element</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> }</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:#bb2ca2">return</span> <span style="color:#bb2ca2">nil</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">}</div></div><div><br></div><div>If I understand correctly, you propose to check the type of the passed-in <span style="color:rgb(112,61,170);font-family:Menlo;font-size:11px">Element</span> in the predicate, like this:</div><div><br></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:#bb2ca2">let</span> result1 = <span style="color:#4f8187">containerView</span>.<span style="color:#703daa">subviews</span>.<span style="color:#31595d">findFirst</span> { ($0 <span style="color:#bb2ca2">as</span>? <span style="color:#703daa">NSButton</span>)?.<span style="color:#703daa">state</span> == <span style="color:#703daa">NSOnState</span> }</div></div><div><br></div><div>But that is cumbersome and has a very significant drawback: The type of <span style="font-family:Menlo;font-size:11px">result1</span> is now <span style="color:rgb(112,61,170);font-family:Menlo;font-size:11px">NSView</span>, not <span style="color:rgb(112,61,170);font-family:Menlo;font-size:11px">NSButton</span>.</div><div>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 <span style="font-family:Menlo;font-size:11px">findFirst()</span> has the type the caller specifies.</div><div>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><br></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(112,61,170)"><span style="color:#bb2ca2">let</span><span style="color:#000000"> result2 = </span><span style="color:#4f8187">containerView</span><span style="color:#000000">.</span>subviews<span style="color:#000000">.</span><span style="color:#31595d">findFirst</span><span style="color:#000000"> { ($0 </span><span style="color:#bb2ca2">as</span><span style="color:#000000">? </span>NSButton<span style="color:#000000">)?.</span>state<span style="color:#000000"> == </span>NSOnState<span style="color:#000000"> } </span><span style="color:#bb2ca2">as</span><span style="color:#000000">? </span>NSButton</div></div><div><br></div><div>I think that’s not very nice compared to how the two generic versions in question look:</div><div><br></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:#bb2ca2">let</span> result3 = <span style="color:#4f8187">containerView</span>.<span style="color:#703daa">subviews</span>.<span style="color:#31595d">findFirst</span>(<span style="color:#703daa">NSButton</span>.<span style="color:#bb2ca2">self</span>, matching: { $0.<span style="color:#703daa">state</span> == <span style="color:#703daa">NSOnState</span> })</div></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-family:Helvetica;font-size:12px">or:</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:rgb(187,44,162)">let</span> result4: <span style="color:rgb(112,61,170)">NSButton</span>? = <span style="color:rgb(79,129,135)">containerView</span>.<span style="color:rgb(112,61,170)">subviews</span>.<span style="color:rgb(49,89,93)">findFirst</span> { $0.<span style="color:rgb(112,61,170)">state</span> == <span style="color:rgb(112,61,170)">NSOnState</span> }</div></div></blockquote></div><br></div>