New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
More natural derived properties #63
Comments
The proxy looks fantastic. If I had a choice between the two, I'd rather go with the proxy and suffer the loss of not being able to use it with non-object properties. However, would it be impractical to offer both? Subscripting primarily for scalars, and proxies for objects? |
I like the proxy much less now that I realize subscribables would always have to be cast. I think I would honestly rather see them wrapped in a dummy object made specifically for this purpose, like: self.titleLabel.rac.textColor = someSubscribable.propertySomethingOrOther; Where the subscribable's property would simply return Can go you into more detail about how exactly the keyed subscripting works? |
@jspahrsummers Yeah I agree the cast is weak sauce :( The keyed subscripting works something like: #define RAC(keypath) self[RAC_KEYPATH_SELF(keypath)]
- (void)setObject:(id)obj forKeyedSubscript:(id<NSCopying>)key {
[self rac_deriveProperty:(NSString *) key from:obj];
}
RAC(self.blah.thing) = subscribable; The really annoying bit is that the compiler has to be able to see the definition of |
@joshaber So that wouldn't work for objects that aren't relative to |
@jspahrsummers Well, you could imagine a version that does: #define RAC_OBJ(obj, keypath) obj[RAC_KEYPATH(obj, keypath)] But that object would need to to implement Honestly, I don't really care much about that case. The case I'm specifically thinking of is UI where there are a lot of values derived from a few basic values, most/all of which are on the class itself. |
Wait, couldn't you make the proxy-bounce situation way nicer by defining |
Ah, wait, totally misunderstood the problem; nevermind. I've got nothing. |
Actually I think there's probably a way to make the keyed subscripting version more generic. I'll give a try. The proxy bounce is really cool since it lets you pass in a subscribable in the place of a normal object but maybe there's a better / more specific way to surface that. |
Also, what happens if you proxy bounce on a Does it have to be a subscribable of subscribables? How would it know? |
That linked PR adds the subscripting-abusive assignment. It's a little more abusive but it works without needing the class to implement @jspahrsummers I'm pretty sure the universe implodes. Pretty sure. |
I've found when doing reactive UI stuff, I end up using
-rac_deriveProperty:from:
a lot. It's awesome for the most part, but it doesn't read very naturally. What I really want to write and read is something like:I've come up with two different ways to write something more like that, both with tradeoffs, so I'd like to get some feedback.
Keyed subscripting:
rac(self.titleLabel.textColor) = someSubscribable;
That abuses keyed subscripting. It works for both object and non-object properties. The downside is that the class has to implement
-setObject:forKeyedSubscript:
. We can create a macro that does it for them, but it's an extra step users have to remember per-class.Proxy bounce
This uses a proxy to bounce message sends through. It's kinda awesome because you can use it with methods that aren't just plain setters, which happens to be the case a fair amount in UIKit land. But this approach has two downsides: (1) you have to cast the subscribable, (2) it won't work with non-object properties.
So I'd be curious to see if anyone has any other ideas or feels strongly about either of those two options.
The text was updated successfully, but these errors were encountered: