Kotlin: Apply vs With

2019-01-21 12:07发布

问题:

What is the difference between with and apply. From what I know the following code does the same thing:

swingElement.apply {
    minWidth = ENABLED_COLUMN_WIDTH
    maxWidth = ENABLED_COLUMN_WIDTH
    preferredWidth = ENABLED_COLUMN_WIDTH
}
with(swingElement) {
    minWidth = ENABLED_COLUMN_WIDTH
    maxWidth = ENABLED_COLUMN_WIDTH
    preferredWidth = ENABLED_COLUMN_WIDTH
}

Is there any difference and should I use one over the other? Also, are there some cases where one would work and the other won't?

回答1:

There're two differences:

  1. apply accepts an instance as the receiver while with requires an instance to be passed as an argument. In both cases the instance will become this within a block.

  2. apply returns the receiver and with returns a result of the last expression within its block.

I'm not sure there can be some strict rules on which function to choose. Usually you use apply when you need to do something with an object and return it. And when you need to perform some operations on an object and return some other object you can use either with or run. I prefer run because it's more readable in my opinion but it's a matter of taste.



回答2:

The apply function

//returns receiver T, T exposed as `this`
fun <T> T.apply(block: T.() -> Unit): T 

Description

The apply function is invoked on a receiver T, which will be exposed as this in the passed lambda expression. The receiver also becomes the result of apply automatically.

The with function

//return arbitrary value R, not an extension function, T exposed as `this` 
fun <T, R> with(receiver: T, block: T.() -> R): R 

Description

The with function, as opposed to all other scope functions (let, run, also, apply), is not defined as an extension function. Instead, the function is invoked with a receiver object as its first argument explicitly. Same as apply, the receiver is exposed as this in the passed lambda. The result of the lambda, i.e. it’s last statement, becomes the result (R) of with.



回答3:

Here are the Similarities and Differences

Similarities

With and Apply both accept an object as a receiver in whatever manner they are passed.

Differences

With returns the last line in the lambda as the result of the expression.

Apply returns the object that was passed in as the receiver as the result of the lambda expression.

Examples

With

private val ORIENTATIONS = with(SparseIntArray()) {
    append(Surface.ROTATION_0, 90)
    append(Surface.ROTATION_90, 0)
    append(Surface.ROTATION_180, 270)
    append(Surface.ROTATION_270, 180)
}
ORIENTATIONS[0] // doesn't work 
// Here, using with prevents me from accessing the items in the SparseArray because, 
// the last line actually returns nothing

Apply

private val ORIENTATIONS = SparseIntArray().apply {
    append(Surface.ROTATION_0, 90)
    append(Surface.ROTATION_90, 0)
    append(Surface.ROTATION_180, 270)
    append(Surface.ROTATION_270, 180)
}
ORIENTATIONS[0] // Works
// Here, using apply, allows me to access the items in the SparseArray because, 
// the SparseArray is returned as the result of the expression


标签: kotlin