https://kotlinlang.org logo
#getting-started
Title
# getting-started
d

Darryl Miles

03/15/2024, 7:15 PM
Any ideas how to make this prototype work:
Copy code
fun myMethod(): java.util.function.Function<Int,String> {
   return {  value: Int ->
      if(value == 42) "42" else ""
   } as Function<Int,String>  // heh
}
r

Ruckus

03/15/2024, 7:24 PM
Use a SAM conversion:
Copy code
fun myMethod(): Function<Int, String> {
    return Function { value: Int ->
        if (value == 42) "42" else ""
    }
}
Or shorter:
Copy code
fun myMethod(): Function<Int, String> =
    Function { if (it == 42) "42" else "" }
d

Darryl Miles

03/15/2024, 7:27 PM
Perfect that compiles nicely, thanks!
👍 1
👍🏿 1