Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Usage of "with" function

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

like image 986
United121 Avatar asked Oct 18 '15 13:10

United121


People also ask

What does with () do in R?

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.

Why do we use with function in R?

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 .

How do you use the word function in a sentence?

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.

How do you use an IF and function in Excel?

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)


Video Answer


1 Answers

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()
like image 69
voddan Avatar answered Nov 01 '22 11:11

voddan