<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div><br>Thanks Brent. Was that real code? &nbsp;:-)</div><div id="AppleMailSignature"><br></div><div id="AppleMailSignature">If it was, I did not realize it was possible to do something like that in Swift. That seems very cool, attractive and powerful. It certainly would look much better with the sugary syntax.&nbsp;</div><div id="AppleMailSignature"><br></div><div id="AppleMailSignature">I can imagine building a collection of sort orderings like this for example.&nbsp;</div><div id="AppleMailSignature"><br></div><div id="AppleMailSignature">// Order by lastName asc, firstName asc</div><div id="AppleMailSignature">let orderings = Person.lastName.asc().then(.firstName.asc())</div><div><br></div><div>Don't know if it would be possible but I think it would be nice to be able to do it. The asc() method would create an ascending sort ordering and would establish Root as Person. &nbsp;I wonder if it would be possible to have the then() know that Root is person so that its argument can be a key path relative to Person. &nbsp;Or maybe this can be expressed differently if this is not the right way.&nbsp;</div><div><br></div><div>Anyways, the sugary syntax looks very attractive IMHO.&nbsp;</div><div><br></div><div>This is what my Java code looks like for something like that:</div><div><br></div><div>NSArray&lt;EOSortOrdering&gt; orderings = Person.LAST_NAME.asc().then(Person.FIRST_NAME.asc());</div><div><br></div><div>or this:</div><div><br></div><div><span style="background-color: rgba(255, 255, 255, 0);">NSArray&lt;EOSortOrdering&gt; orderings =&nbsp;</span></div><div><span style="background-color: rgba(255, 255, 255, 0);">&nbsp; &nbsp; new NSArray&lt;&gt;(</span></div><div><span style="background-color: rgba(255, 255, 255, 0);">&nbsp; &nbsp; &nbsp; &nbsp; Person.LAST_NAME.asc(),</span></div><div><span style="background-color: rgba(255, 255, 255, 0);">&nbsp; &nbsp; &nbsp; &nbsp; Person.FIRST_NAME.asc()</span></div><div><span style="background-color: rgba(255, 255, 255, 0);">&nbsp; &nbsp; );</span></div><div><br></div><div><br></div><div>On Apr 1, 2017, at 10:24 PM, Brent Royal-Gordon &lt;<a href="mailto:brent@architechies.com">brent@architechies.com</a>&gt; wrote:<br><br></div><blockquote type="cite"><div><meta http-equiv="Content-Type" content="text/html charset=utf-8"><br class=""><div><blockquote type="cite" class=""><div class="">On Apr 1, 2017, at 5:56 PM, Karl Wagner 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 class="" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><div class=""><blockquote type="cite" class="" style="font-family: Helvetica; font-size: 12px; 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-size-adjust: auto; -webkit-text-stroke-width: 0px;"><div class=""><div class="" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><div class=""><div class=""><div class=""><font face="Monaco" class="" style="font-size: 12px;">let isPuppyQualifier = Pet.type.equals(.dog).and(Pet.age.lessThan(12))</font></div><div class=""><font face="Monaco" class="" style="font-size: 12px;">let familyQualifier = Family.pets.hasAtLeastOne(satisfying: isPuppyQualifier)</font></div><div class=""><div class=""><font face="Monaco" class="" style="font-size: 12px;">let familiesWithPuppies = Family.fetch(editingContext, familyQualifier)</font></div></div><div class=""><div class=""><br class=""></div></div><div class="">For those unfamiliar with EOF, the<span class="Apple-converted-space">&nbsp;</span><font face="Monaco" class=""><span class="" style="font-size: 12px;">editingContext</span></font>&nbsp;in the code above is an<span class="Apple-converted-space">&nbsp;</span><font face="Monaco" class=""><span class="" style="font-size: 12px;">EOEditingContext</span></font><span class="Apple-converted-space">&nbsp;</span>which is analogous to<span class="Apple-converted-space">&nbsp;</span><font face="Monaco" class=""><span class="" style="font-size: 12px;">NSManagedObjectContext</span></font>.&nbsp;</div></div></div></div></div></blockquote><br class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;"></div><div class="">Theoretically, you could do something like that with this proposal...</div><div class=""><br class=""></div><blockquote class="" style="margin: 0px 0px 0px 40px; border: none; padding: 0px;"><div class=""><font face="Courier" class="">struct UnaryPredicate&lt;Parameter, Result&gt; {</font></div><div class=""><font face="Courier" class="">&nbsp; &nbsp; let evaluate: (Parameter) -&gt; Result</font></div><div class=""><font face="Courier" class="">}</font></div><div class=""><div class=""><font face="Courier" class="">struct BinaryPredicate&lt;Left, Right, Result&gt; {</font></div></div><div class=""><div class=""><font face="Courier" class="">&nbsp; &nbsp; let evaluate: (Left, Right) -&gt; Result</font></div></div><div class=""><div class=""><font face="Courier" class="">}</font></div></div><div class=""><font face="Courier" class=""><br class=""></font></div><div class=""><font face="Courier" class="">extension KeyPath where Value: Equatable {</font></div><div class=""><font face="Courier" class="">&nbsp; &nbsp; func equals(_ value: Value) -&gt; UnaryPredicate&lt;Root, Bool&gt; {</font></div><div class=""><font face="Courier" class="">&nbsp; &nbsp; &nbsp; &nbsp; return UnaryPredicate { $0[keyPath: self] == value }</font></div><div class=""><font face="Courier" class="">&nbsp; &nbsp; }</font></div><div class=""><div class=""><font face="Courier" class="">&nbsp; &nbsp; func equals&lt;KP: KeyPath&gt;(_ other: KP) -&gt; BinaryPredicate&lt;Root, KP.Root, Bool&gt; where KP.Value == Value {</font></div></div><div class=""><div class=""><font face="Courier" class="">&nbsp; &nbsp; &nbsp; &nbsp; return BinaryPredicate { $0[keyPath: self] == $1[keyPath: other] }</font></div></div><div class=""><div class=""><font face="Courier" class="">&nbsp; &nbsp; }</font></div></div><div class=""><font face="Courier" class="">}</font></div><div class=""><font face="Courier" class=""><br class=""></font></div><div class=""><font face="Courier" class="">let isDog = #keypath(Pet, .type).equals(.dog) // UnaryPredicate&lt;Pet, Bool&gt;</font></div><div class=""><font face="Courier" class="">if isDog.evaluate(somePet) {</font></div><div class=""><font face="Courier" class="">&nbsp; &nbsp; print(“It’s a dog”)</font></div><div class=""><font face="Courier" class="">}</font></div><div class=""><br class=""></div><div class=""><font face="Courier" class="">let areSameLength = #keypath(Array&lt;Int&gt;, .count).equals(#keypath(Array&lt;String&gt;, .count))</font></div><div class=""><span class="" style="font-family: Courier;">// BinaryPredicate&lt;Array&lt;Int&gt;, Array&lt;String&gt;, Bool&gt;</span></div><div class=""><font face="Courier" class="">if areSameLength.evaluate([1,2,3], [“a”, “b”, “c”]) {</font></div><div class=""><font face="Courier" class="">&nbsp; &nbsp; print(“same lengths”)</font></div><div class=""><font face="Courier" class="">}</font></div></blockquote></div></div></blockquote></div><div class=""><br class=""></div><div class="">You guys aren't thinking big enough.</div><div class=""><br class=""></div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>// This implementation is closure-based because it's too complex for an email even&nbsp;*with*&nbsp;</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>//&nbsp;generics system upgrades. Without type system upgrades, things get *really* complicated,&nbsp;</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>// though probably still tractable.</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>typealias Predicate&lt;Element&gt; = (Element) -&gt; Bool</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span></div><div class=""><div><span class="Apple-tab-span" style="white-space: pre;">        </span>func == &lt;Root, Value: Equatable&gt; (lhs: KeyPath&lt;Root, Value&gt;, rhs: Value) -&gt; Predicate&lt;Root&gt; {</div><div><span class="Apple-tab-span" style="white-space: pre;">                </span>return { $0[keyPath: lhs] == rhs }</div></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>}</div><div class=""><div><span class="Apple-tab-span" style="white-space: pre;">        </span></div></div><div><div class=""><div><span class="Apple-tab-span" style="white-space: pre;">        </span>func &lt; &lt;Root, Value: Comparable&gt; (lhs: KeyPath&lt;Root, Value&gt;, rhs: Value) -&gt; Predicate&lt;Root&gt; {</div><div><span class="Apple-tab-span" style="white-space: pre;">                </span>return { $0[keyPath: lhs] &lt; rhs }</div></div><div><span class="Apple-tab-span" style="white-space: pre;">        </span>}</div><div class=""><div><br class=""></div></div><div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>func &amp;&amp; &lt;Root&gt;(lhs: Predicate&lt;Root&gt;, rhs: Predicate&lt;Root&gt;) -&gt; Predicate&lt;Root&gt; {</div><div class=""><span class="Apple-tab-span" style="white-space:pre">                </span>return { lhs($0) &amp;&amp; rhs($0) }</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>}</div><div class=""><br class=""></div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>extension KeyPath where Value: Collection {</div><div class=""><span class="Apple-tab-span" style="white-space:pre">                </span>func contains(where predicate: Predicate&lt;Value.Element&gt;) -&gt; Predicate&lt;Value&gt; {</div><div class=""><span class="Apple-tab-span" style="white-space:pre">                        </span>return { $0.contains(where: predicate) }</div><div class=""><span class="Apple-tab-span" style="white-space:pre">                </span>}</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>}</div><div class=""><br class=""></div><div class="">That gives you:</div><div class=""><br class=""></div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>let isPuppyQualifier = #keyPath(Pet, .type) == .dog &amp;&amp; #keyPath(Pet, .age) &lt; 12</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>let familyQualifier = #keyPath(Family, .pets).contains(where: isPuppyQualifier)</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>let familiesWithPuppies = Family.fetch(editingContext, familyQualifier)</div><div class=""><br class=""></div><div class="">Or, in one line with a more sugary syntax:</div><div class=""><br class=""></div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>let familiesWithPuppies = Family.fetch(editingContext, (.pets).contains(where: .type == .dog &amp;&amp; .age &lt; 12))</div></div></div><br class=""><div 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 class=""><div style="font-size: 12px; " class="">--&nbsp;</div><div style="font-size: 12px; " class="">Brent Royal-Gordon</div><div style="font-size: 12px; " class="">Architechies</div></div></span>

</div>
<br class=""></div></blockquote></body></html>