Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 783
blackpanther Avatar asked Aug 17 '17 09:08

blackpanther


People also ask

What is the purpose of {} squiggly braces in Java?

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.

What are the curly brackets {{ }} used for?

Curly brackets are commonly used in programming languages such as C, Java, Perl, and PHP to enclose groups of statements or blocks of code.

What are the curly braces in API?

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.

Are curly brackets necessary in Java?

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.


1 Answers

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 }) 
like image 160
BakaWaii Avatar answered Oct 01 '22 03:10

BakaWaii