queryAttributes() type-safe interface does not compile #356

Closed
opened 2025-12-29 15:29:55 +01:00 by adam · 1 comment
Owner

Originally created by @H2CO3 on GitHub (Jan 2, 2021).

I'm trying to use DataStack.queryAttributes() with key paths. I have the following entity definition:

final class Region: CoreStoreObject {
    @Field.Stored("regionID")
    var regionID: Int64 = 0
    
    @Field.Stored("name")
    var name: String = ""
}

I tried to call queryAttributes in several different ways based on the documentation, but I couldn't get it to compile. Using string literals works, but I would like to use key paths.

Version information: macOS 11, Swift 5.3.2.

Attempt no. 1:

func queryContinents() throws -> [[String: Any]] {
    return try dataStack.queryAttributes(
        From<Region>()
            .select(\.regionID, \.name)
    )
}

Compiler error:

Sources/Queries.swift:28:25: error: cannot infer key path type from context; consider explicitly specifying a root type
                .select(\.regionID, \.name)
                        ^
                         <#Root#>
Sources/Queries.swift:28:37: error: cannot infer key path type from context; consider explicitly specifying a root type
                .select(\.regionID, \.name)
                                    ^
                                     <#Root#>

Attempt no. 2:

func queryContinents() throws -> [[String: Any]] {
    return try dataStack.queryAttributes(
        From<Region>()
            .select(.attribute(\.regionID), .attribute(\.name))
    )
}

Compiler error:

Sources/Queries.swift:28:26: error: type 'NSDictionary.Type' has no member 'attribute'
                .select(.attribute(\.regionID), .attribute(\.name))
                        ~^~~~~~~~~~~~~~~~~~~~~
Sources/Queries.swift:28:36: error: cannot infer key path type from context; consider explicitly specifying a root type
                .select(.attribute(\.regionID), .attribute(\.name))
                                   ^
                                    <#Root#>
Sources/Queries.swift:28:50: error: referencing static method 'attribute' on 'SelectTerm' requires that 'Region' inherit from 'NSManagedObject'
                .select(.attribute(\.regionID), .attribute(\.name))
                                                 ^
/Users/H2CO3/Documents/egyetem/PhD/Demos/SwiftCoreStore/Dependencies/CoreStore/Sources/Select.swift:310:1: note: where 'O' = 'Region'
extension SelectTerm where O: NSManagedObject {

Attempt no. 3:

func queryContinents() throws -> [[String: Any]] {
    return try dataStack.queryAttributes(
        From<Region>(),
        Select(.attribute(\.regionID), .attribute(\.name))
    )
}

Compiler error:

Sources/Queries.swift:28:21: error: referencing static method 'attribute' on 'SelectTerm' requires that 'Region' inherit from 'NSManagedObject'
            Select(.attribute(\.regionID), .attribute(\.name))
                    ^
/Users/H2CO3/Documents/egyetem/PhD/Demos/SwiftCoreStore/Dependencies/CoreStore/Sources/Select.swift:310:1: note: where 'O' = 'Region'
extension SelectTerm where O: NSManagedObject {
^
Sources/Queries.swift:28:45: error: referencing static method 'attribute' on 'SelectTerm' requires that 'Region' inherit from 'NSManagedObject'
            Select(.attribute(\.regionID), .attribute(\.name))
                                            ^
/Users/H2CO3/Documents/egyetem/PhD/Demos/SwiftCoreStore/Dependencies/CoreStore/Sources/Select.swift:310:1: note: where 'O' = 'Region'
extension SelectTerm where O: NSManagedObject {

Attempt no. 4:

func queryContinents() throws -> [[String: Any]] {
    return try dataStack.queryAttributes(
        From<Region>(),
        Select(\.regionID, \.name)
    )
}

Compiler error:

Sources/Queries.swift:28:20: error: cannot infer key path type from context; consider explicitly specifying a root type
            Select(\.regionID, \.name)
                   ^
                    <#Root#>
Sources/Queries.swift:28:32: error: cannot infer key path type from context; consider explicitly specifying a root type
            Select(\.regionID, \.name)
                               ^
                                <#Root#>

Attempt no. 5:

func queryContinents() throws -> [[String: Any]] {
    return try dataStack.queryAttributes(
        From<Region>(),
        Select(\Region.regionID, \Region.name)
    )
}

Compiler error:

Sources/Queries.swift:28:20: error: value of type 'CoreStoreObject' has no member 'regionID'
            Select(\Region.regionID, \Region.name)
                   ^~~~~~~~~~~~~~~~
Sources/Queries.swift:28:38: error: value of type 'CoreStoreObject' has no member 'name'
            Select(\Region.regionID, \Region.name)
                                     ^~~~~~~~~~~~

I've tried a few variants as well, eventually all of them resulted in some of the errors enumerated above. What am I missing?

Originally created by @H2CO3 on GitHub (Jan 2, 2021). I'm trying to use `DataStack.queryAttributes()` with key paths. I have the following entity definition: ```swift final class Region: CoreStoreObject { @Field.Stored("regionID") var regionID: Int64 = 0 @Field.Stored("name") var name: String = "" } ``` I tried to call `queryAttributes` in several different ways based on the documentation, but I couldn't get it to compile. Using string literals works, but I would like to use key paths. Version information: macOS 11, Swift 5.3.2. ## Attempt no. 1: ```swift func queryContinents() throws -> [[String: Any]] { return try dataStack.queryAttributes( From<Region>() .select(\.regionID, \.name) ) } ``` Compiler error: ``` Sources/Queries.swift:28:25: error: cannot infer key path type from context; consider explicitly specifying a root type .select(\.regionID, \.name) ^ <#Root#> Sources/Queries.swift:28:37: error: cannot infer key path type from context; consider explicitly specifying a root type .select(\.regionID, \.name) ^ <#Root#> ``` ## Attempt no. 2: ```swift func queryContinents() throws -> [[String: Any]] { return try dataStack.queryAttributes( From<Region>() .select(.attribute(\.regionID), .attribute(\.name)) ) } ``` Compiler error: ``` Sources/Queries.swift:28:26: error: type 'NSDictionary.Type' has no member 'attribute' .select(.attribute(\.regionID), .attribute(\.name)) ~^~~~~~~~~~~~~~~~~~~~~ Sources/Queries.swift:28:36: error: cannot infer key path type from context; consider explicitly specifying a root type .select(.attribute(\.regionID), .attribute(\.name)) ^ <#Root#> Sources/Queries.swift:28:50: error: referencing static method 'attribute' on 'SelectTerm' requires that 'Region' inherit from 'NSManagedObject' .select(.attribute(\.regionID), .attribute(\.name)) ^ /Users/H2CO3/Documents/egyetem/PhD/Demos/SwiftCoreStore/Dependencies/CoreStore/Sources/Select.swift:310:1: note: where 'O' = 'Region' extension SelectTerm where O: NSManagedObject { ``` ## Attempt no. 3: ```swift func queryContinents() throws -> [[String: Any]] { return try dataStack.queryAttributes( From<Region>(), Select(.attribute(\.regionID), .attribute(\.name)) ) } ``` Compiler error: ``` Sources/Queries.swift:28:21: error: referencing static method 'attribute' on 'SelectTerm' requires that 'Region' inherit from 'NSManagedObject' Select(.attribute(\.regionID), .attribute(\.name)) ^ /Users/H2CO3/Documents/egyetem/PhD/Demos/SwiftCoreStore/Dependencies/CoreStore/Sources/Select.swift:310:1: note: where 'O' = 'Region' extension SelectTerm where O: NSManagedObject { ^ Sources/Queries.swift:28:45: error: referencing static method 'attribute' on 'SelectTerm' requires that 'Region' inherit from 'NSManagedObject' Select(.attribute(\.regionID), .attribute(\.name)) ^ /Users/H2CO3/Documents/egyetem/PhD/Demos/SwiftCoreStore/Dependencies/CoreStore/Sources/Select.swift:310:1: note: where 'O' = 'Region' extension SelectTerm where O: NSManagedObject { ``` ## Attempt no. 4: ```swift func queryContinents() throws -> [[String: Any]] { return try dataStack.queryAttributes( From<Region>(), Select(\.regionID, \.name) ) } ``` Compiler error: ``` Sources/Queries.swift:28:20: error: cannot infer key path type from context; consider explicitly specifying a root type Select(\.regionID, \.name) ^ <#Root#> Sources/Queries.swift:28:32: error: cannot infer key path type from context; consider explicitly specifying a root type Select(\.regionID, \.name) ^ <#Root#> ``` ## Attempt no. 5: ```swift func queryContinents() throws -> [[String: Any]] { return try dataStack.queryAttributes( From<Region>(), Select(\Region.regionID, \Region.name) ) } ``` Compiler error: ``` Sources/Queries.swift:28:20: error: value of type 'CoreStoreObject' has no member 'regionID' Select(\Region.regionID, \Region.name) ^~~~~~~~~~~~~~~~ Sources/Queries.swift:28:38: error: value of type 'CoreStoreObject' has no member 'name' Select(\Region.regionID, \Region.name) ^~~~~~~~~~~~ ``` I've tried a few variants as well, eventually all of them resulted in some of the errors enumerated above. What am I missing?
adam closed this issue 2025-12-29 15:29:55 +01:00
Author
Owner

@H2CO3 commented on GitHub (Jan 2, 2021):

It looks like the README isn't updated – using the new \.$property syntax, it worked. For future reference, here's the example that showed me the correct usage: 3d82ee18c7/Demo/%E2%AD%90%EF%B8%8FSources/%E2%AD%90%EF%B8%8FDemos/%E2%AD%90%EF%B8%8FModern/%E2%AD%90%EF%B8%8FTimeZonesDemo/%E2%AD%90%EF%B8%8FModern.TimeZonesDemo.MainView.swift (L105)

@H2CO3 commented on GitHub (Jan 2, 2021): It looks like the README isn't updated – using the new `\.$property` syntax, it worked. For future reference, here's the example that showed me the correct usage: https://github.com/JohnEstropia/CoreStore/blob/3d82ee18c7457b41d0f1f389b5e46eeb95623408/Demo/%E2%AD%90%EF%B8%8FSources/%E2%AD%90%EF%B8%8FDemos/%E2%AD%90%EF%B8%8FModern/%E2%AD%90%EF%B8%8FTimeZonesDemo/%E2%AD%90%EF%B8%8FModern.TimeZonesDemo.MainView.swift#L105
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/CoreStore#356