<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body><div>On Fri, Apr 29, 2016, at 05:12 PM, Max Moiseev via swift-evolution wrote:<br></div>
<blockquote type="cite"><div>HI all,<br></div>
<div>&nbsp;</div>
<div>&nbsp;</div>
<div>After having discussed this proposal with the members of the standard library team, we would like to propose the following updates:<br></div>
<div>&nbsp;</div>
<div>- Since both the language and the standard library have evolved since it was written, the proposal should reflect these changes (things like renaming `Generator` to `Iterator`, adjusting for first argument label rules etc.)<br></div>
<blockquote style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:40px;border-top-style:none;border-right-style:none;border-bottom-style:none;border-left-style:none;border-top-width:initial;border-right-width:initial;border-bottom-width:initial;border-left-width:initial;border-top-color:initial;border-right-color:initial;border-bottom-color:initial;border-left-color:initial;border-image-source:initial;border-image-slice:initial;border-image-width:initial;border-image-outset:initial;border-image-repeat:initial;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;"><div>(Actually, while writing this, I discovered&nbsp;<a href="https://github.com/apple/swift-evolution/pull/276">https://github.com/apple/swift-evolution/pull/276</a>)<br></div>
</blockquote><div>- We believe that renaming `find(_:)` to `first(where:)` would make call sites more clear<br></div>
<div>&nbsp;</div>
<blockquote style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:40px;border-top-style:none;border-right-style:none;border-bottom-style:none;border-left-style:none;border-top-width:initial;border-right-width:initial;border-bottom-width:initial;border-left-width:initial;border-top-color:initial;border-right-color:initial;border-bottom-color:initial;border-left-color:initial;border-image-source:initial;border-image-slice:initial;border-image-width:initial;border-image-outset:initial;border-image-repeat:initial;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;"><div><div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(88, 126, 168);"><div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;color:rgb(207, 135, 36);"><span style="font-variant-ligatures:no-common-ligatures;">// original proposal</span><br></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;"><span style="font-variant-ligatures:no-common-ligatures;">numbers</span><span class="colour" style="color:rgb(0, 0, 0)">.</span><span style="font-variant-ligatures:no-common-ligatures;">find</span><span class="colour" style="color:rgb(0, 0, 0)"> { </span><span style="font-variant-ligatures:no-common-ligatures;">isPrime</span><span class="colour" style="color:rgb(0, 0, 0)">($0) }</span><br></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;"><span style="font-variant-ligatures:no-common-ligatures;">numbers</span><span class="colour" style="color:rgb(0, 0, 0)">.</span><span style="font-variant-ligatures:no-common-ligatures;">find</span><span class="colour" style="color:rgb(0, 0, 0)">(</span><span style="font-variant-ligatures:no-common-ligatures;">isPrime</span><span class="colour" style="color:rgb(0, 0, 0)">)</span><br></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;min-height:14px;">&nbsp;</div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;color:rgb(207, 135, 36);"><span style="font-variant-ligatures:no-common-ligatures;">// suggested update</span><br></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;"><span style="font-variant-ligatures:no-common-ligatures;">numbers</span><span class="colour" style="color:rgb(0, 0, 0)">.</span><span style="font-variant-ligatures:no-common-ligatures;">first</span><span class="colour" style="color:rgb(0, 0, 0)"> { </span><span style="font-variant-ligatures:no-common-ligatures;">isPrime</span><span class="colour" style="color:rgb(0, 0, 0)">($0) }</span><br></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;"><span style="font-variant-ligatures:no-common-ligatures;">numbers</span><span class="colour" style="color:rgb(0, 0, 0)">.</span><span style="font-variant-ligatures:no-common-ligatures;">first</span><span class="colour" style="color:rgb(0, 0, 0)">(</span><span class="colour" style="color:rgb(53, 86, 138)">where</span><span class="colour" style="color:rgb(0, 0, 0)">: </span><span style="font-variant-ligatures:no-common-ligatures;">isPrime</span><span class="colour" style="color:rgb(0, 0, 0)">)</span><br></div>
<div>&nbsp;</div>
</div>
</div>
</blockquote><blockquote style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:40px;border-top-style:none;border-right-style:none;border-bottom-style:none;border-left-style:none;border-top-width:initial;border-right-width:initial;border-bottom-width:initial;border-left-width:initial;border-top-color:initial;border-right-color:initial;border-bottom-color:initial;border-left-color:initial;border-image-source:initial;border-image-slice:initial;border-image-width:initial;border-image-outset:initial;border-image-repeat:initial;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;"><div>In the examples above, when the predicate is passed as a trailing closure, there is no big difference in the invocation, but it changes when we have a named function that we would like to use as a predicate.<br></div>
<div>The Collection protocol already has a property called `first`, that returns an optional element, it also has a method `index(where:)`. In this sense `first(where:)` does not introduce new words to the library vocabulary.<br></div>
</blockquote></blockquote><div>&nbsp;</div>
<div>first(where:) is a neat idea, but I'm a little concerned about ambiguity with the property in the presence of type errors. Experimentally, if I try to call first(where:) with a block with the wrong signature, Swift 3 produces an unhelpful error about how I cannot call value of a non-function type (i.e. the property) instead of recognizing that I'm trying to call the function.<br></div>
<div>&nbsp;</div>
<div>Example:<br></div>
<div>&nbsp;</div>
<div>struct Foo {<br></div>
<div>&nbsp;&nbsp;&nbsp; var first: Int = 42<br></div>
<div>&nbsp;</div>
<div>&nbsp;&nbsp;&nbsp; func first(where: @noescape Int -&gt; Bool) -&gt; Int {<br></div>
<div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return 42<br></div>
<div>&nbsp;&nbsp;&nbsp; }<br></div>
<div>}<br></div>
<div>&nbsp;</div>
<div>If I try and call `print(Foo().find(where: { true }))`, you'll note that the closure has the wrong type signature (it types as `() -&gt; Bool`), and the compiler gives me the error:<br></div>
<div>&nbsp;</div>
<div>&lt;REPL Input&gt;:1:12: error: cannot call value of non-function type 'Int'<br></div>
<div>Foo().first(where: { true })<br></div>
<div>~~~~~~~~~~~^<br></div>
<div>&nbsp;</div>
<div>Calling it with a block of the correct type works, but having useful errors is very important.</div>
<div>&nbsp;</div>
<div>-Kevin Ballard</div>
</body>
</html>