is there somebody who can explain me what "with" function is used for?
Signature
public inline fun <T, R> with(receiver: T, f: T.() -> R): R = receiver.f()
Doc
Calls the specified function f with the given receiver as its receiver and returns its result.
And I found its using on this project Antonio Leiva. It was using for moving view :
fun View.animateTranslationY(translationY: Int, interpolator: Interpolator) {
with(ObjectAnimator.ofFloat(this, "translationY", translationY.toFloat())) {
setDuration(context.resources.getInteger(R.integer.config_mediumAnimTime).toLong())
setInterpolator(interpolator)
start()
}
}
I was thinking that I know the meaning to I transfer it to
fun View.animateTranslationX(translationX: Int, interpolator: Interpolator) {
with(ObjectAnimator()) {
ofFloat(this, "translationX", translationX.toFloat())
setDuration(context.resources.getInteger(R.integer.config_mediumAnimTime).toLong())
setInterpolator(interpolator)
start()
}
}
but it doesn't compile ... But I think that ObjectAnimaton
is receiver and it get everything what I will call in {}
bracket. Can anybody explain the real meaning and provide a basic example - at least more basic than this? :D
R with() function That is with() function enables us to evaluate an R expression within the function to be passed as an argument. It works on data frames only. That is why the outcome of the evaluation of the R expression is done with respect to the data frame passed to it as an argument.
Use with whenever you like interactively (R console) and in R scripts to save typing and make your code clearer. The more frequently you would need to re-type your data frame name for a single command (and the longer your data frame name is!), the greater the benefit of using with .
They went to several functions during their college reunion weekend. Verb The new machine functions well. His bad health has prevented him from being able to function effectively in recent weeks. Her heart now seems to be functioning normally.
When you combine each one of them with an IF statement, they read like this: AND – =IF(AND(Something is True, Something else is True), Value if True, Value if False) OR – =IF(OR(Something is True, Something else is True), Value if True, Value if False) NOT – =IF(NOT(Something is True), Value if True, Value if False)
The idea is the same as with
keyword in Pascal.
Anyway, here are three samples with identical semantic:
with(x) {
bar()
foo()
}
with(x) {
this.bar()
this.foo()
}
x.bar()
x.foo()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With