<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div>On Jan 14, 2016, at 10:33 AM, Joe Groff via swift-evolution <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>> wrote:</div><div><br></div><blockquote type="cite"><blockquote type="cite"><span>One more thing: should there be a way to pass arbitrary data, rather than initializers or accessors, into a behavior? For instance, it would be nice if you could do something like:</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span> var behavior backedByJSON<Value: JSONRepresentable where Self: JSONObjectRepresentable>: Value {</span><br></blockquote><blockquote type="cite"><span> var key: String</span><br></blockquote><blockquote type="cite"><span> </span><br></blockquote><blockquote type="cite"><span> init(key: String) {</span><br></blockquote><blockquote type="cite"><span> backedByJSON.key = key</span><br></blockquote><blockquote type="cite"><span> }</span><br></blockquote><blockquote type="cite"><span> </span><br></blockquote><blockquote type="cite"><span> get {</span><br></blockquote><blockquote type="cite"><span> return Value(JSON: self.JSON[key])</span><br></blockquote><blockquote type="cite"><span> }</span><br></blockquote><blockquote type="cite"><span> set {</span><br></blockquote><blockquote type="cite"><span> self.JSON[key] = newValue.JSON</span><br></blockquote><blockquote type="cite"><span> }</span><br></blockquote><blockquote type="cite"><span> }</span><br></blockquote><blockquote type="cite"><span> </span><br></blockquote><blockquote type="cite"><span> struct User: JSONObjectRepresentable {</span><br></blockquote><blockquote type="cite"><span> var JSON: [String: JSONType]</span><br></blockquote><blockquote type="cite"><span> </span><br></blockquote><blockquote type="cite"><span> var [backedByJSON(key: "id")] ID: Int</span><br></blockquote><blockquote type="cite"><span> var [backedByJSON(key: "name")] name: String</span><br></blockquote><blockquote type="cite"><span> </span><br></blockquote><blockquote type="cite"><span> var [backedByJSON(key: "posts")] posts: [Post]</span><br></blockquote><blockquote type="cite"><span> }</span><div style="display: none;"><br></div></blockquote><span></span><br><span></span><br><span>That parameterization could be achieved, somewhat more verbosely, using an accessor 'key { return "id" }'. If behaviors could be parameterized in-line, I wouldn't do it by passing the params to `init`, since that again imposes the need for per-instance storage to carry `key` from the behavior initialization to the property implementation.</span></blockquote><div><br></div><div>It would be very handy to have access to the property's name inside the behavior. As proposed, we have Self and self for referring to the type and instance of the container and newValue for referring to the new value in the core getter. A predefined local variable "name" of type String, bound to the name of the decorated property, would be handy for implementing things like Brent's backedByJSON behavior:</div><div><br></div><div><div><font color="#000000"><span style="background-color: rgba(255, 255, 255, 0);"> var behavior backedByJSON<Value: JSONRepresentable where Self: JSONObjectRepresentable>: Value {</span></font></div><div><font color="#000000"><span style="background-color: rgba(255, 255, 255, 0);"> get {<br></span></font></div><div><font color="#000000"><span style="background-color: rgba(255, 255, 255, 0);"> return Value(JSON: self.JSON[name])<br></span></font></div><div><font color="#000000"><span style="background-color: rgba(255, 255, 255, 0);"> }<br></span></font></div><div><font color="#000000"><span style="background-color: rgba(255, 255, 255, 0);"> set {<br></span></font></div><div><font color="#000000"><span style="background-color: rgba(255, 255, 255, 0);"> self.JSON[name] = newValue.JSON<br></span></font></div><div><font color="#000000"><span style="background-color: rgba(255, 255, 255, 0);"> }<br></span></font></div><div><font color="#000000"><span style="background-color: rgba(255, 255, 255, 0);"> }</span></font></div></div><div><br></div><div>A "name" property could also be used for an object-level observation system that included the name of the changed property in published notifications. Such a system can be useful, for example, in constructing delta transaction for sending to a remote API. (E.g., we could notify a server that the due date of a task has changed rather than sending the whole task object or performing a diff.)</div><div><br></div><div>Adding this bit of magic would go a long way toward letting us implement something like OAAppearance[1] in Swift instead of importing runtime.h and dynamically reifying methods[2]. </div><div><br></div><div>Cheers,</div><div><br></div><div>Curt</div><div><br></div><div>Curt Clifton, PhD</div><div>Software Developer</div><div>The Omni Group</div><div><br></div><div>[1] - <a href="https://github.com/omnigroup/OmniGroup/tree/master/Frameworks/OmniAppKit/Appearance">https://github.com/omnigroup/OmniGroup/tree/master/Frameworks/OmniAppKit/Appearance</a></div><div>[2] - <a href="http://curtclifton.net/video-of-oaappearance-at-xcoders">http://curtclifton.net/video-of-oaappearance-at-xcoders</a></div><div><br></div><div><br></div></body></html>