<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 &lt;<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>&gt; 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> &nbsp; &nbsp;var behavior backedByJSON&lt;Value: JSONRepresentable where Self: JSONObjectRepresentable&gt;: Value {</span><br></blockquote><blockquote type="cite"><span> &nbsp; &nbsp; &nbsp; &nbsp;var key: String</span><br></blockquote><blockquote type="cite"><span> &nbsp; &nbsp; &nbsp; &nbsp;</span><br></blockquote><blockquote type="cite"><span> &nbsp; &nbsp; &nbsp; &nbsp;init(key: String) {</span><br></blockquote><blockquote type="cite"><span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;backedByJSON.key = key</span><br></blockquote><blockquote type="cite"><span> &nbsp; &nbsp; &nbsp; &nbsp;}</span><br></blockquote><blockquote type="cite"><span> &nbsp; &nbsp; &nbsp; &nbsp;</span><br></blockquote><blockquote type="cite"><span> &nbsp; &nbsp; &nbsp; &nbsp;get {</span><br></blockquote><blockquote type="cite"><span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return Value(JSON: self.JSON[key])</span><br></blockquote><blockquote type="cite"><span> &nbsp; &nbsp; &nbsp; &nbsp;}</span><br></blockquote><blockquote type="cite"><span> &nbsp; &nbsp; &nbsp; &nbsp;set {</span><br></blockquote><blockquote type="cite"><span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;self.JSON[key] = newValue.JSON</span><br></blockquote><blockquote type="cite"><span> &nbsp; &nbsp; &nbsp; &nbsp;}</span><br></blockquote><blockquote type="cite"><span> &nbsp; &nbsp;}</span><br></blockquote><blockquote type="cite"><span> &nbsp; &nbsp;</span><br></blockquote><blockquote type="cite"><span> &nbsp; &nbsp;struct User: JSONObjectRepresentable {</span><br></blockquote><blockquote type="cite"><span> &nbsp; &nbsp; &nbsp; &nbsp;var JSON: [String: JSONType]</span><br></blockquote><blockquote type="cite"><span> &nbsp; &nbsp; &nbsp; &nbsp;</span><br></blockquote><blockquote type="cite"><span> &nbsp; &nbsp; &nbsp; &nbsp;var [backedByJSON(key: "id")] ID: Int</span><br></blockquote><blockquote type="cite"><span> &nbsp; &nbsp; &nbsp; &nbsp;var [backedByJSON(key: "name")] name: String</span><br></blockquote><blockquote type="cite"><span> &nbsp; &nbsp; &nbsp; &nbsp;</span><br></blockquote><blockquote type="cite"><span> &nbsp; &nbsp; &nbsp; &nbsp;var [backedByJSON(key: "posts")] posts: [Post]</span><br></blockquote><blockquote type="cite"><span> &nbsp; &nbsp;}</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);">&nbsp; &nbsp;var behavior backedByJSON&lt;Value: JSONRepresentable where Self: JSONObjectRepresentable&gt;: Value {</span></font></div><div><font color="#000000"><span style="background-color: rgba(255, 255, 255, 0);">&nbsp; &nbsp; &nbsp; &nbsp;get {<br></span></font></div><div><font color="#000000"><span style="background-color: rgba(255, 255, 255, 0);">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return Value(JSON: self.JSON[name])<br></span></font></div><div><font color="#000000"><span style="background-color: rgba(255, 255, 255, 0);">&nbsp; &nbsp; &nbsp; &nbsp;}<br></span></font></div><div><font color="#000000"><span style="background-color: rgba(255, 255, 255, 0);">&nbsp; &nbsp; &nbsp; &nbsp;set {<br></span></font></div><div><font color="#000000"><span style="background-color: rgba(255, 255, 255, 0);">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;self.JSON[name] = newValue.JSON<br></span></font></div><div><font color="#000000"><span style="background-color: rgba(255, 255, 255, 0);">&nbsp; &nbsp; &nbsp; &nbsp;}<br></span></font></div><div><font color="#000000"><span style="background-color: rgba(255, 255, 255, 0);">&nbsp; &nbsp;}</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].&nbsp;</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] -&nbsp;<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] -&nbsp;<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>