I don't understand the real difference between the curly braces and and the normal brackets in Kotlin when using RxJava. For example, I have the following code which works as expected:
someMethodThatReturnsCompletable()
.andThen(anotherMethodThatReturnsACompletable())
.subscribe(...)
But the following does NOT work:
someMethodThatReturnsCompletable()
.andThen { anotherMethodThatReturnsACompletable() }
.subscribe(...)
Note the difference in the andThen()
part of the chain with the curly braces. I can't understand what the difference between the two is. I've had a look at some articles but unfortunately I am still having difficulty in understanding this subtle difference.
The first code segment executes anotherMethodThatReturnsACompletable()
and passes the return value to andThen()
, where a Completable
is accepted as parameter.
In the second code segment, you are writing a function literal as a lambda expression. It passes a function of type () -> Unit
to andThen()
, which it also a valid statement, but the code inside the lambda may not be called.
In Kotlin, there is a convention that if the last parameter to a function is a function, and you're passing a lambda expression as the corresponding argument, you can specify it outside of parentheses:
lock (lock) { sharedResource.operation() }
Since Kotlin support SAM conversion,
This means that Kotlin function literals can be automatically converted into implementations of Java interfaces with a single non-default method, as long as the parameter types of the interface method match the parameter types of the Kotlin function.
Looking back to Completable
, there are several overloaded andThen()
functions:
andThen(CompletableSource next)
andThen(MaybeSource<T> next)
andThen(ObservableSource<T> next)
andThen(org.reactivestreams.Publisher<T> next)
andThen(SingleSource<T> next)
Here you can specify the SAM Type by calling:
andThen( CompletableSource {
//implementations
})
As you probably know that in Java ()
brackets are used for passing parameters and {}
braces are used for method body and also represents the body of lambda expression.
So lets compare:
.andThen(anotherMethodThatReturnsACompletable())
: here andThen() method acceptsCompletable
so andThen will save reference to the completable returned byanotherMethodThatReturnsACompletable()
method to subscribe later..andThen { anotherMethodThatReturnsACompletable() }
: This passes lambda expression to andThen method. HereanotherMethodThatReturnsACompletable()
is not invoked at the time of passing the lambda.anotherMethodThatReturnsACompletable()
will be called when lambda function is invoked in andThen method.
Hope it helps.
.andThen(anotherMethodThatReturnsACompletable())
means that the result of anotherMethodThatReturnsACompletable()
will be passed to andThen()
.andThen { anotherMethodThatReturnsACompletable() }
means that the lambda, that executes anotherMethodThatReturnsACompletable()
will be passed to andThen()
()
-> You are passing something inside them i.e. function arguments
{}
-> You are executing something inside them. i.e. expression
来源:https://stackoverflow.com/questions/45731647/whats-the-difference-between-curly-braces-and-normal-brackets-in-rxjava-with-ko