devxoul / Then

✨ Super sweet syntactic sugar for Swift initializers

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[proposal] Implementation of another method without inout

takasek opened this issue · comments

Problem

The code below works in Then 1.0.1 but doesn't work in 1.0.2.

func tempt(label: UILabel) {
    label.text = "I💞Then"
}

let label1 = UILabel()
tempt(label1) // Works fine definitely
UILabel().then(tempt) // Error: Cannot convert value of type '(UILabel) -> ()' to expected argument type 'inout UILabel -> Void'

Adding inout keyword to the func resolves the error, but it brings another problem.

func temptInOut(inout label: UILabel) {
    label.text = "I💞Then"
}

UILabel().then(temptInOut) // Works fine

let label2 = UILabel()
temptInOut(label2) // Error: Cannot convert value of type 'UILabel' to expected argument type 'inout UILabel'
temptInOut(&label2) // Another Error: Cannot pass immutable value as inout argument: 'label2' is a 'let' constant

To resolve all errors, I should write code as below.

var label3 = UILabel()
temptInOut(&label3) // Works fine but seems ugly.

I don't use neither var nor &...

Proposal

#18 is nice-to-have modification, but old-style method(without inout keyword) is also nice in some situations.

So why don't you implement another old-type method as (@noescape block: Self -> Void) -> Self ?

Hi @takasek!
Why do you need UILabel().then(tempt)? As you mentioned, tempt(label1) could be the solution.

In some case, I want to give then a function, instead of a closure.
Consider of code to apply special animation to some buttons with a function below:

func applyInteractionAnimationToButton(button: UIButton) {
    //long and complicated logic...
    ...
}

With Then 1.0.2 , I have to write:

let button = UIButton(frame: ...)
applyInteractionAnimationToButton(button)
self.contentView.addSubview(button)

or

self.contentView.addSubview(
    UIButton(frame: ...).then {
        applyInteractionAnimationToButton($0)
    }
)

But I want to write:

self.contentView.addSubview(
    UIButton(frame: ...).then(applyInteractionAnimationToButton)
)

It is comforting than former codes, isn't it?

@takasek, That's reasonable. I just released a new version 1.0.3 😄