Skip to content
This repository has been archived by the owner on Nov 14, 2018. It is now read-only.

View.post with with this view as a parameter #586

Closed
romtsn opened this issue Jun 24, 2018 · 2 comments
Closed

View.post with with this view as a parameter #586

romtsn opened this issue Jun 24, 2018 · 2 comments

Comments

@romtsn
Copy link
Contributor

romtsn commented Jun 24, 2018

Sometimes it's tedious to call a post method on some View object, and then access this object again inside the braces, especially with databinding. For example:

binding.included.toolbar.post {
    binding.included.toolbar.subtitle = "something"
}

If we'd have a following extension, it could make it a little bit easier:

inline fun <reified T : View> T.postOnView(crossinline action: (T) -> Unit): Runnable

// usage
binding.included.toolbar.postOnView { it.subtitle = "something" }
@JakeWharton
Copy link
Contributor

My general problem with this is that the majority of APIs accepting some kind of callback or lambda do not provide the enclosing instance and we would have to apply this trick to all of them.

The traditional Kotlin pattern for solving this is to create a scope with the instance you want to re-use with let:

binding.included.toolbar.let { toolbar ->
  toolbar.post { toolbar.subtitle = "something" }
}

@romtsn
Copy link
Contributor Author

romtsn commented Jun 24, 2018

Good point, also with is a good candidate for this. Just thought that the post is the most usable thing, which could've had such an extension to ease the usage for those, who is not aware of scoping functions/doesn't want to use them for reasons.

@romtsn romtsn closed this as completed Jun 29, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants