<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">On 16 May 2016, at 16:25, Erica Sadun &lt;<a href="mailto:erica@ericasadun.com" class="">erica@ericasadun.com</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=us-ascii" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class=""><blockquote type="cite" class=""><div class="">On May 8, 2016, at 10:33 AM, Karl Wagner via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div class=""><blockquote type="cite" class=""><div class="">On 4 May 2016, at 00:46, Aleksandar Petrovic via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div class="">Hi swift-evolution,<br class=""><br class="">I want to apologize in advance for my clumsy English. It's (obviously) not my first language.<br class=""><br class="">Recent discussion about property behaviours reminded me of function decorators in Python. I think decorators can nicely fit in Swift, too.<br class=""><br class="">First, a bit of explanation for the uninitiated. Decorator is a function that transform other function - it receives some function and returns a function of the same signature. Lets make some dead simple decorator:<br class=""></div></div></blockquote></div></div></div></blockquote></div><br class=""><div class=""><br class=""></div><div class=""><div class="">In Objective-C, you can add a category that will entirely replace an existing method. Could using decorators allow us to add behaviors to methods to extend their behavior without overwriting those methods? Here is an example of what I'm thinking.</div><div class=""><br class=""></div><div class="">extension UIView {</div><div class="">&nbsp; &nbsp;decorate func somethingElse(...) {</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// performs existing behavior of somethingElse</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; ... adds new behavior ...</div><div class="">&nbsp; &nbsp;}</div><div class="">}</div><div class=""><br class=""></div><div class="">Context: I was asking about whether this kind of "extend rather than replace or subclass" behavior were possible in Swift, and was told that "decorators" might be the right technology to implement it.</div><div class=""><br class=""></div><div class="">Thanks in advance for any insight.</div><div class=""><br class=""></div><div class="">-- E</div><div class=""><br class=""></div><div class=""><blockquote type="cite" class=""></blockquote></div></div></div></div></blockquote></div><br class=""><div class="">I was thinking more along the lines of property behaviours, which are specified at compile-time. Decorating an existing function inside an extension would only be possible for overridable functions in non-final types (otherwise the functions may have been inlined). But yeah, theoretically I think you could capture the existing function pointer and replace it in the vtable with a decorated one. That is basically what you’d do in Objective-C today (getting the IMP for a selector, making an IMP from a closure and switch them in the dispatch table, then call original IMP from your closure).</div></body></html>