[swift-evolution] [Review #2] SE-0161: Smart KeyPaths: Better Key-Value Coding for Swift

Haravikk swift-evolution at haravikk.me
Sat Apr 8 16:47:55 CDT 2017

> On 8 Apr 2017, at 22:18, BJ Homer via swift-evolution <swift-evolution at swift.org> wrote:
> I love the idea of a leading and trailing backslash. It makes it much easier to read, and handles the "but what if I want to access a property of a KeyPath?" case really well.
> For example, these two are clearly distinct:
>   let x = \Person.mother.age\.valueType
>   let y = \Person.mother.age.valueType\
> I'm not sure why an 'age' object would have a 'valueType' property, but this variant makes it easy to handle. Even in cases where no disambiguation is required, having the trailing backslash makes it much easier to read as I'm scanning through code.
> -BJ

I think I'd prefer brackets for that case; in your first example that reads to me like an escape of the period character, rather than "this is the end of the key path". Brackets would make this consistent with escaping within strings, like so:

	let x = \(Person.mother.age).valueType
	let y = \(Person.mother.age.valueType)

Which makes sense to me if you consider the backslash in this case being an escape from normal type/variable access. Put another way, normally when you type Person. you're telling Swift "access this type and look for static methods/properties etc.", whereas when you "escape" it you're telling Swift to not to do that, resulting in a key-path instead.

Maybe it's a stretch, it that makes sense logically to me, plus I think the use of brackets just looks cleaner than another backslash.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170408/a1604d96/attachment.html>

More information about the swift-evolution mailing list