Unimplemented types in Swift 4 KeyPaths


(Brett Walker) #1

Hi all,

In the Swift 4 Keypaths proposal [0161] there seem to be several types that are unimplemented (or not working as I expect them to), and I'm not sure whether these are simply unfinished in Beta 1, or just ideas that weren't accepted.

For instance, according to the proposal AnyKeyPath is supposed to be fully type-erased and access properties as optionals at run-time, but using it I only get the compile-time error "Type "FooClass" has no subscript members", even when applied to objects that still have the original type of the KeyPath's root.

Even if I use PartialKeyPath<Root> I get the same error:

···

--
var tester = TestClass(name: "steve")
let tester_path = \TestClass.name

let partial_path:PartialKeyPath<TestClass> = tester_path
let name = tester[keyPath: partial_path] /// no subscript members error here
--

Only when I either use the original path variable, or cast it to KeyPath<TestClass, String>, am I able to access the property without an error. I am wanting to use KeyPaths in a more generic way, so the AnyKeyPath and PartialKeyPath would be helpful. Does anyone have insight into the status of these KeyPath types?

regards,
-brett


(Joe Groff) #2

Support for these did not make it into Xcode 9 beta 1. They are available in swift.org snapshot toolchains, if you want to experiment with them now, and should be in a future seed of Xcode 9.

-Joe

···

On Jun 19, 2017, at 5:23 PM, Brett Walker via swift-users <swift-users@swift.org> wrote:

Hi all,

In the Swift 4 Keypaths proposal [0161] there seem to be several types that are unimplemented (or not working as I expect them to), and I'm not sure whether these are simply unfinished in Beta 1, or just ideas that weren't accepted.

For instance, according to the proposal AnyKeyPath is supposed to be fully type-erased and access properties as optionals at run-time, but using it I only get the compile-time error "Type "FooClass" has no subscript members", even when applied to objects that still have the original type of the KeyPath's root.

Even if I use PartialKeyPath<Root> I get the same error:
--
var tester = TestClass(name: "steve")
let tester_path = \TestClass.name

let partial_path:PartialKeyPath<TestClass> = tester_path
let name = tester[keyPath: partial_path] /// no subscript members error here
--

Only when I either use the original path variable, or cast it to KeyPath<TestClass, String>, am I able to access the property without an error. I am wanting to use KeyPaths in a more generic way, so the AnyKeyPath and PartialKeyPath would be helpful. Does anyone have insight into the status of these KeyPath types?


(Brett Walker) #3

Thanks for the information, Joe. I'll try to dig into the snapshot. Hopefully they'll actually work in the generic way I need them to!

regards,
-brett

···

On 6/21/17 10:05 AM, Joe Groff wrote:

Support for these did not make it into Xcode 9 beta 1. They are available in swift.org snapshot toolchains, if you want to experiment with them now, and should be in a future seed of Xcode 9.

-Joe