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.
Different programming languages have various ways to delineate the start and end points of a programming structure, such as a loop, method or conditional statement. For example, Java and C++ are often referred to as curly brace languages because curly braces are used to define the start and end of a code block.
Curly brackets are commonly used in programming languages such as C, Java, Perl, and PHP to enclose groups of statements or blocks of code.
Curly braces are used to define "dictionary literals," giving you the ability to declare a dictionary and its contents inside your program. A dictionary literal consists of a series of key/value pairs, written with a colon (:) between them, and with each of the key/value pairs separated from one other with commas.
Always include curly brackets, even for one-line if statements or for loops. Java has a "feature" which allows you to omit curly brackets when writing if statements, for loops, or while loops containing only a single statement. You should never use this feature – always include curly brackets.
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 })
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