[swift-evolution] Smart KeyPaths

Matt Diephouse matt+swift-evolution at diephouse.com
Sat Mar 18 11:37:10 CDT 2017

> On Mar 17, 2017, at 6:38 PM, Joe Groff via swift-evolution <swift-evolution at swift.org> wrote:
>> On Mar 17, 2017, at 12:34 PM, David Hart via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>> Sent off-list by mistake:
>> Nice proposal. I have a few comments inline:
>>> On 17 Mar 2017, at 18:04, Michael LeHew via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>>> Use/Mention Distinctions
>>> While methods can be referred to without invoking them (let x = foo.bar instead of  let x = foo.bar()), this is not currently possible for properties and subscripts.
>>> Making indirect references to a properties' concrete types also lets us expose metadata about the property, and in the future additional behaviors.
>> What metadata is attached? How is it accessed? What future features are you thinking about?
> To begin with, you'd have limited ability to stringify a key path. Eventually we'd like to support other reflectiony things, including:
> - Asking for the primary key paths a type supports
> - Asking for a key by name or index
> - Breaking a key path down by components

This might be my new favorite Swift feature. <3 It seems obvious in retrospect, which is surely a sign of its genius.

I would LOVE to be able to get the individual components in a path. Even if it were just the names, that would let me do some things that I’ve been itching to do in Swift. If the types were exposed in a type-erased container, that would be even better.

Something like this would be plenty of information to accomplish my goals:

    class AnyKeyPath {

        struct Key {
            let name: String
            let rootType: Any.Type
            let valueType: Any.Type

        private(set) var keys: [Key]

But even stringifying a key path would probably let me do most of what I wanted. I’d just need to (somewhat awkwardly) split on `.`s and handle subscripts. You mentioned that we’d have “limited” ability to stringify a key path. Can you elaborate on what that means or what the limitations would be?


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170318/8cab8f01/attachment.html>

More information about the swift-evolution mailing list