https://kotlinlang.org logo
Title
m

Milan Hruban

08/31/2019, 8:42 AM
Hello, I have a java function with following signature:
public static ScoreDto score(String email, String name, String surname, Function<String, Boolean> function)
What would be the idiomatic/more readable approach to call it from Kotlin, given that
dataProvider
is a nullable instance of class that implements Function<String, Boolean>?
score(
        "<mailto:some_email@domain.com|some_email@domain.com>",
        "John",
        "Doe"
    ) { dataProvider?.apply(it) }
vs
score(
        "<mailto:some_email@domain.com|some_email@domain.com>",
        "John",
        "Doe",
        { dataProvider?.apply(it) }
     )
vs
dataProvider?.let {
    score(
       "<mailto:some_email@domain.com|some_email@domain.com>"
        "John",
        "Doe",
        it
    )
}
s

SiebelsTim

08/31/2019, 6:39 PM
The last one has different semantics than the first two. It doesn't call the function if the provider is null. The first is more idiomatic than the second I'd say.
1
m

Milan Hruban

09/01/2019, 6:03 AM
oh I didn't even realize that. Thank you
m

Matteo Mirk

09/02/2019, 2:00 PM
The first form is definitely the most idiomatic. — side note: the last parameter in the Java signature could be simplified to
Predicate<String> function
.
❤️ 1