> On 16 May 2016, at 16:25, Erica Sadun <[email protected]> wrote:
>
>> On May 8, 2016, at 10:33 AM, Karl Wagner via swift-evolution
>> <[email protected] <mailto:[email protected]>> wrote:
>>
>>
>>> On 4 May 2016, at 00:46, Aleksandar Petrovic via swift-evolution
>>> <[email protected] <mailto:[email protected]>> wrote:
>>>
>>> Hi swift-evolution,
>>>
>>> I want to apologize in advance for my clumsy English. It's (obviously) not
>>> my first language.
>>>
>>> Recent discussion about property behaviours reminded me of function
>>> decorators in Python. I think decorators can nicely fit in Swift, too.
>>>
>>> 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:
>
>
> 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.
>
> extension UIView {
> decorate func somethingElse(...) {
> // performs existing behavior of somethingElse
> ... adds new behavior ...
> }
> }
>
> 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.
>
> Thanks in advance for any insight.
>
> -- E
>
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).
_______________________________________________
swift-evolution mailing list
[email protected]
https://lists.swift.org/mailman/listinfo/swift-evolution