Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[proposal] Implementation of another method without inout #25

Closed
takasek opened this issue Jun 1, 2016 · 3 comments
Closed

[proposal] Implementation of another method without inout #25

takasek opened this issue Jun 1, 2016 · 3 comments

Comments

@takasek
Copy link

takasek commented Jun 1, 2016

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 ?

@devxoul
Copy link
Owner

devxoul commented Jun 1, 2016

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

@takasek
Copy link
Author

takasek commented Jun 1, 2016

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?

@devxoul
Copy link
Owner

devxoul commented Jun 4, 2016

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

@devxoul devxoul closed this as completed Jun 4, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants