[swift-evolution] [Proposal/Pitch] Function decorators

Erica Sadun erica at ericasadun.com
Mon May 16 09:25:08 CDT 2016


> On May 8, 2016, at 10:33 AM, Karl Wagner via swift-evolution <swift-evolution at swift.org> wrote:
> 
> 
>> On 4 May 2016, at 00:46, Aleksandar Petrovic via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> 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

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160516/34b56fb7/attachment.html>


More information about the swift-evolution mailing list