<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=""><div><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class=""><div class=""><div class="">I wish CoreData (or similar object-to-relational-mapping module) in Swift eventually leverage smart keys and key paths to make our code beautiful when building sort orderings, qualifiers (predicates or whatever they are called in CoreData) to build queries or apply them in-memory to arrays. This is what I do every day.</div><div class=""><br class=""></div><div class="">For example, wouldn't it look much nicer if some day you could express the above like this in Swift:</div><div class=""><br class=""></div><div class=""><font face="Monaco" style="font-size: 12px;" class="">let isPuppyQualifier = Pet.type.equals(.dog).and(Pet.age.lessThan(12))</font></div><div class=""><font face="Monaco" style="font-size: 12px;" class="">let familyQualifier = Family.pets.hasAtLeastOne(satisfying: isPuppyQualifier)</font></div><div class=""><div class=""><font face="Monaco" style="font-size: 12px;" class="">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 <font face="Monaco" class=""><span style="font-size: 12px;" class="">editingContext</span></font> in the code above is an <font face="Monaco" class=""><span style="font-size: 12px;" class="">EOEditingContext</span></font> which is analogous to <font face="Monaco" class=""><span style="font-size: 12px;" class="">NSManagedObjectContext</span></font>. </div></div></div></div></div></blockquote><br class=""></div><div>Theoretically, you could do something like that with this proposal...</div><div><br class=""></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div><font face="Courier" class="">struct UnaryPredicate<Parameter, Result> {</font></div><div><font face="Courier" class=""> let evaluate: (Parameter) -> Result</font></div><div><font face="Courier" class="">}</font></div><div><div><font face="Courier" class="">struct BinaryPredicate<Left, Right, Result> {</font></div></div><div><div><font face="Courier" class=""> let evaluate: (Left, Right) -> Result</font></div></div><div><div><font face="Courier" class="">}</font></div></div><div><font face="Courier" class=""><br class=""></font></div><div><font face="Courier" class="">extension KeyPath where Value: Equatable {</font></div><div><font face="Courier" class=""> func equals(_ value: Value) -> UnaryPredicate<Root, Bool> {</font></div><div><font face="Courier" class=""> return UnaryPredicate { $0[keyPath: self] == value }</font></div><div><font face="Courier" class=""> }</font></div><div><div><font face="Courier" class=""> func equals<KP: KeyPath>(_ other: KP) -> BinaryPredicate<Root, KP.Root, Bool> where KP.Value == Value {</font></div></div><div><div><font face="Courier" class=""> return BinaryPredicate { $0[keyPath: self] == $1[keyPath: other] }</font></div></div><div><div><font face="Courier" class=""> }</font></div></div><div><font face="Courier" class="">}</font></div><div><font face="Courier" class=""><br class=""></font></div><div><font face="Courier" class="">let isDog = #keypath(Pet, .type).equals(.dog) // UnaryPredicate<Pet, Bool></font></div><div><font face="Courier" class="">if isDog.evaluate(somePet) {</font></div><div><font face="Courier" class=""> print(“It’s a dog”)</font></div><div><font face="Courier" class="">}</font></div><div><br class=""></div><div><font face="Courier" class="">let areSameLength = #keypath(Array<Int>, .count).equals(#keypath(Array<String>, .count))</font></div><div><span style="font-family: Courier;" class="">// BinaryPredicate<Array<Int>, Array<String>, Bool></span></div><div><font face="Courier" class="">if areSameLength.evaluate([1,2,3], [“a”, “b”, “c”]) {</font></div><div><font face="Courier" class=""> print(“same lengths”)</font></div><div><font face="Courier" class="">}</font></div></blockquote><div><br class=""></div>but the syntax isn’t as nice when you start chaining key paths together because #keypath() is a little loud. No doubt there will be plenty of libraries for this sort of thing, though.<div class=""><br class=""></div><div class="">- Karl</div></body></html>