[swift-evolution] SE-0064: Referencing the Objective-C selector of property getters and setters

William Jon Shipley wjs at delicious-monster.com
Thu Apr 7 18:48:28 CDT 2016

What is your evaluation of the proposal?

Strong yes.

Is the problem being addressed significant enough to warrant a change to Swift?

At Delicious we wrote a huge ugly macro set to define properties in a special way so we could make key paths safely:

// Declares a safe-KVC-accessible property
// Use as: @property (opts) MyType KVC(*name);
// Use as: @property (opts) MyType KVC2(*firstName, *lastName);
#define KVC(a) \
    a; SAFE_KVC(a)

#define SAFE_KVC(NAME) \
    void NAME ## $DMSafeKVC(void)

#define K(x) \
    __builtin_choose_expr(sizeof(&x ## $DMSafeKVC), @#x, @"_NOT_A_KEY_")

So you could then use K(title) for example in your code and get what would now be #keyPath(title) in this proposal.

And we also had:

#define KeyPath(...) \
    DMMakeKeyPath(__VA_ARGS__, nil)

NSString *DMMakeKeyPath(NSString *firstKey, ...)
    NSMutableArray *keyArray = [NSMutableArray arrayWithObject:firstKey];
    va_list varargs;
    va_start(varargs, firstKey);
    NSString *nextKey = nil;
    while ((nextKey = va_arg(varargs, __unsafe_unretained NSString *)))
        [keyArray addObject:nextKey];
    return [keyArray componentsJoinedByString:@"."];

So we could make paths.

But this whole system was ugly as heck to read and required to you use ugly macros when defining properties AND when making key paths, so I wouldn’t use it again if I had to do it all again. I’d just like to demonstrate crazy lengths people have gone to to get SOME of this functionality.


I am curious how this proposal integrates with “KVO2.” Not that we’re talking about that here, but it’s something I imagine Apple is thinking about, so I’m not sure how much value my opinion is without knowing what’s coming.

(As an aside, gosh it’d be nice if other groups writing APIs had such a wonderful review process like the Swift group.)

Does this proposal fit well with the feel and direction of Swift?

I think so. Swift is about safety, and this make key paths much safer. And more readable as key paths instead of as strings.

If you have used other languages or libraries with a similar feature, how do you feel that this proposal compares to those?

The horrifying one we wrote was much inferior yet we shipped a hundred thousand lines or so on it.

How much effort did you put into your review? A glance, a quick reading, or an in-depth study?

Quick reading. I’m not sure if everything in it is actually implementable and I don’t really care what the syntax is, I just want a way to do this cleanly.

Your pal,

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160407/03cc423a/attachment.html>

More information about the swift-evolution mailing list