Solution 1 :
It’s just the syntax differences between the two languages for how to write a lambda.
Java lambdas were unfortunately added long after the original language was already designed, so for backward compatibility it’s a bit harder to read.
But maybe you’re just asking if you can put the lambda inside the function parentheses? You can still do that in Kotlin, but the compiler will give you a warning because it’s less readable.
val postalCode: LiveData<String> = Transformations.switchMap(addressInput, {
address -> repository.getPostCode(address) })
Also, your first piece of code is not a function declaration…it’s a property assignment. Your Kotlin property assigns it one time at class initialization, whereas your Java method creates a new instance each time it’s called.
Problem :
This is a Kotlin function declaration
val postalCode: LiveData<String> = Transformations.switchMap(addressInput) {
address -> repository.getPostCode(address) }
This is a Java function declaration
public final LiveData<String> postalCode(){
Transformations.switchMap(addressInput, (address) -> {
return repository.getPostCode(address);
})};
1 – Why is not possible to declare in Kotlin something like this :
... .(addressInput, (address) -> {
return repository.getPostCode(address);
})
,pleae show how is posible to make a delaration like Java.
Comments
Comment posted by Slaw
Why would you expect two different languages to have the
Comment posted by Vasile
because is the same function that is waiting for 2 parameters. For me is not clear why in Kotlin is not so clear like in java that we give this 2 parametrs
Comment posted by Vasile
switchMap is waiting for 2 parameters. In Java it’s clear we give 2 parameters. but in Kotlin we give 2 parameters but they are not visually in one scope. Or you think that with time i will be used with Kotlin style and will not put such questions ?
Comment posted by things like this
@Vasile I
Comment posted by Tenfour04
The trailing lambda being outside the parentheses is used over and over in Kotlin. You will be used to it after spending a couple hours reading and writing Kotlin code and you will find it tedious going back to Java and having to keep track of trailing parentheses all over the place.