[swift-evolution] Proposal: 'selfless' keyword for refactoring duplicate code from initialisers

Charles Srstka cocoadev at charlessoft.com
Tue Dec 15 19:48:02 CST 2015


> On Dec 15, 2015, at 7:06 PM, Marc Knaup <marc at knaup.koeln> wrote:
> 
> In its current state with the initial example -1 from me for the proposal.
> 
> The example could easily be written like this:
> 
> class FooView: UIView {
> 
>     var property = 4
> 
>     init() {
>         super.init()
>     }
> 
>     init(frame: CGRect) {
>         super.init(frame)
>     }
> }
> 
> 
> In cases where the initially value is computed in a complex way a closure can be used:
> 
> class FooView: UIView {
> 
>     var property: Int = {
>         // some complicated computation
>         return value
>     }()
> 
>     init() {
>         super.init()
>     }
> 
>     init(frame: CGRect) {
>         super.init(frame)
>     }
> }

That works for simplistic cases such as this. However, if you have a large number of properties that all need to be set, and if their values are all interconnected in some way, dependent on some of the initializer’s parameters, or dependent on some value computed at runtime, this pattern cannot accomplish the task cleanly.

Charles

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151215/404daadc/attachment.html>


More information about the swift-evolution mailing list