Clause building via keypaths does not work for CoreStoreObject subclasses #291

Closed
opened 2025-12-29 18:25:21 +01:00 by adam · 2 comments
Owner

Originally created by @manmal on GitHub (Oct 18, 2019).

Hi, first of all, thanks for the great work. This is one of the cleanest and most innovative Swift libraries I've seen.. e.g. the keypath handling is mind boggling :)

I'm having an issue using keypaths for Where clauses on entity subclasses (the Animal > Dog situation), namely when querying fields that reside in the super object. This affects not only Where clauses, but for simplicity I picked this example. I isolated it and made a demo project demonstrating the issue. But I think you just need this piece of code:

import CoreStore
import Foundation

class Animal: CoreStoreObject {
    let animalProperty = Value.Required<String>("animalProperty", initial: "initial")
}

class Dog: Animal {
    let dogProperty = Value.Required<String>("dogProperty", initial: "initial")
}

struct Fetcher {

    // Works
    static func fetchDogByDogProperty() throws -> Dog? {
        return try CoreStore.fetchOne(
            From<Dog>()
                .where(\.dogProperty == "query")
        )
    }

    // Error "Type of expression is ambiguous without more context" in line 26
    static func fetchDogByAnimalProperty() throws -> Dog? {
        return try CoreStore.fetchOne(
            From<Dog>()
                .where(\.animalProperty == "query")
        )
    }

    // Error "Cannot convert value of type 'Where<Animal>' to expected argument type 'Where<_>'" in line 34
    static func fetchDogByAnimalPropertyTry2() throws -> Dog? {
        return try CoreStore.fetchOne(
            From<Dog>()
                .where(\Dog.animalProperty == "query")
        )
    }
}

I'm using Xcode 11 with Swift 5.

Originally created by @manmal on GitHub (Oct 18, 2019). Hi, first of all, thanks for the great work. This is one of the cleanest and most innovative Swift libraries I've seen.. e.g. the keypath handling is mind boggling :) I'm having an issue using keypaths for `Where` clauses on entity subclasses (the `Animal` > `Dog` situation), namely when querying fields that reside in the super object. This affects not only Where clauses, but for simplicity I picked this example. I isolated it and made a demo project demonstrating the issue. But I think you just need this piece of code: ```swift import CoreStore import Foundation class Animal: CoreStoreObject { let animalProperty = Value.Required<String>("animalProperty", initial: "initial") } class Dog: Animal { let dogProperty = Value.Required<String>("dogProperty", initial: "initial") } struct Fetcher { // Works static func fetchDogByDogProperty() throws -> Dog? { return try CoreStore.fetchOne( From<Dog>() .where(\.dogProperty == "query") ) } // Error "Type of expression is ambiguous without more context" in line 26 static func fetchDogByAnimalProperty() throws -> Dog? { return try CoreStore.fetchOne( From<Dog>() .where(\.animalProperty == "query") ) } // Error "Cannot convert value of type 'Where<Animal>' to expected argument type 'Where<_>'" in line 34 static func fetchDogByAnimalPropertyTry2() throws -> Dog? { return try CoreStore.fetchOne( From<Dog>() .where(\Dog.animalProperty == "query") ) } } ``` I'm using Xcode 11 with Swift 5.
adam added the question label 2025-12-29 18:25:21 +01:00
adam closed this issue 2025-12-29 18:25:21 +01:00
Author
Owner

@JohnEstropia commented on GitHub (Oct 18, 2019):

@manmal Yes, this is a known limitation with how KeyPaths work.
For these cases, you can use the closure version instead:

.where({ $0.animalProperty == "query" })
@JohnEstropia commented on GitHub (Oct 18, 2019): @manmal Yes, this is a known limitation with how `KeyPath`s work. For these cases, you can use the closure version instead: ```swift .where({ $0.animalProperty == "query" }) ```
Author
Owner

@manmal commented on GitHub (Oct 18, 2019):

Thank you @JohnEstropia !

@manmal commented on GitHub (Oct 18, 2019): Thank you @JohnEstropia !
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/CoreStore-JohnEstropia#291