<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 Apr 1, 2017, at 5:56 PM, Karl Wagner via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> 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"> </span><font face="Monaco" class=""><span class="" style="font-size: 12px;">editingContext</span></font> in the code above is an<span class="Apple-converted-space"> </span><font face="Monaco" class=""><span class="" style="font-size: 12px;">EOEditingContext</span></font><span class="Apple-converted-space"> </span>which is analogous to<span class="Apple-converted-space"> </span><font face="Monaco" class=""><span class="" style="font-size: 12px;">NSManagedObjectContext</span></font>. </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<Parameter, Result> {</font></div><div class=""><font face="Courier" class=""> let evaluate: (Parameter) -> Result</font></div><div class=""><font face="Courier" class="">}</font></div><div class=""><div class=""><font face="Courier" class="">struct BinaryPredicate<Left, Right, Result> {</font></div></div><div class=""><div class=""><font face="Courier" class=""> let evaluate: (Left, Right) -> 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=""> func equals(_ value: Value) -> UnaryPredicate<Root, Bool> {</font></div><div class=""><font face="Courier" class=""> return UnaryPredicate { $0[keyPath: self] == value }</font></div><div class=""><font face="Courier" class=""> }</font></div><div class=""><div class=""><font face="Courier" class=""> func equals<KP: KeyPath>(_ other: KP) -> BinaryPredicate<Root, KP.Root, Bool> where KP.Value == Value {</font></div></div><div class=""><div class=""><font face="Courier" class=""> return BinaryPredicate { $0[keyPath: self] == $1[keyPath: other] }</font></div></div><div class=""><div class=""><font face="Courier" class=""> }</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<Pet, Bool></font></div><div class=""><font face="Courier" class="">if isDog.evaluate(somePet) {</font></div><div class=""><font face="Courier" class=""> 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<Int>, .count).equals(#keypath(Array<String>, .count))</font></div><div class=""><span class="" style="font-family: Courier;">// BinaryPredicate<Array<Int>, Array<String>, Bool></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=""> 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 *with* </div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>// generics system upgrades. Without type system upgrades, things get *really* complicated, </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<Element> = (Element) -> 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 == <Root, Value: Equatable> (lhs: KeyPath<Root, Value>, rhs: Value) -> Predicate<Root> {</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 < <Root, Value: Comparable> (lhs: KeyPath<Root, Value>, rhs: Value) -> Predicate<Root> {</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><br class=""></div></div><div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>func && <Root>(lhs: Predicate<Root>, rhs: Predicate<Root>) -> Predicate<Root> {</div><div class=""><span class="Apple-tab-span" style="white-space:pre">                </span>return { lhs($0) && 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<Value.Element>) -> Predicate<Value> {</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 && #keyPath(Pet, .age) < 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 && .age < 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="">-- </div><div style="font-size: 12px; " class="">Brent Royal-Gordon</div><div style="font-size: 12px; " class="">Architechies</div></div></span>
</div>
<br class=""></body></html>