What's the difference between curly braces and normal brackets in RxJava with Kotlin

天涯浪子 提交于 2019-12-04 16:45:36

问题


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.


回答1:


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
})



回答2:


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:

  1. .andThen(anotherMethodThatReturnsACompletable()) : here andThen() method accepts Completable so andThen will save reference to the completable returned by anotherMethodThatReturnsACompletable() method to subscribe later.

  2. .andThen { anotherMethodThatReturnsACompletable() } : This passes lambda expression to andThen method. Here anotherMethodThatReturnsACompletable() 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.




回答3:


.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()




回答4:


() -> 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!