https://kotlinlang.org logo
#announcements
Title
# announcements
i

Ifvwm

07/19/2019, 2:32 AM
EventCallback<Float> { if (it > 0) println("ouch!") else println("1") } I don't understand the syntax, put a type before a lambda, is ?
s

Shawn

07/19/2019, 2:37 AM
How familiar are you with generics
you’re not putting a type before a lambda, you’re passing in a type parameter to a genericized function
i

Ifvwm

07/19/2019, 2:42 AM
I know generics, but I don't know how generics work with lambda
s

Shawn

07/19/2019, 2:42 AM
have you looked at this particular function definition?
if I had to guess it probably looks something like this
Copy code
fun <T> EventCallback<T>(block: (T) -> Unit)
i

Ifvwm

07/19/2019, 2:43 AM
"""
object : EventCallback<Float> { override fun onEvent(arg0: Float): Unit = if (arg0 > 0) println("ouch!") else println("1")
s

Shawn

07/19/2019, 2:45 AM
that looks more accurate, actually,
EventCallback<T>
would be an interface
in this case a SAM interface
you also want to use triple backticks ` ``` ` to wrap code, and shift-enter to insert newlines without sending messages
i

Ifvwm

07/19/2019, 2:46 AM
Copy code
object : EventCallback<Float> {
            override fun onEvent(arg0: Float): Unit = if (arg0 > 0) println("ouch!")
             else println("1")
I don't know this stuff turn to
Copy code
EventCallback<Float> { if (it > 0) println("ouch!") else println("1") }
s

Shawn

07/19/2019, 2:47 AM
so if you ctrl/cmd+b into EventCallback you’ll see the definition of the interface
probably looks like this if I had to guess
Copy code
public interface EventCallback<T> {
  void onEvent(T arg0);
}
with a single method defined, making it a Single Abstract Method interface, which can be converted to a lambda
i

Ifvwm

07/19/2019, 2:49 AM
aha,
s

Shawn

07/19/2019, 2:49 AM
Kotlin lets you implement SAMs with a single lambda because it can substitute in for that single method
you need to provide a parameter for
T
if it can’t be inferred
i

Ifvwm

07/19/2019, 2:50 AM
ok
s

Shawn

07/19/2019, 2:50 AM
you could probably do something like this if you wanted
Copy code
EventCallback { param: Float ->
  if (param > 0) println("ouch!") else println("1")
}
i

Ifvwm

07/19/2019, 2:51 AM
what this `it' here means?
should it be param?
you really should read through the docs if you haven’t already
i

Ifvwm

07/19/2019, 2:55 AM
fine
but your code is ```
Copy code
param: Float ->
  if (it > 0)
s

Shawn

07/19/2019, 2:58 AM
That might not work actually, depends on if type inference is smart enough in this case
i

Ifvwm

07/19/2019, 3:00 AM
I thought it should be
Copy code
it: Float ->
if (it>0)
s

Shawn

07/19/2019, 3:01 AM
you can explicitly type
it
or you can pass an explicit parameter to the interface
ah I see
I typo’d, my bad
i

Ifvwm

07/19/2019, 3:02 AM
this lambda stuff in kotlin seems wonderful!
s

Shawn

07/19/2019, 3:03 AM
They’re pretty great, I’d consider their use to be a defining feature of the language
i

Ifvwm

07/19/2019, 3:08 AM
aha "That's because Kotlin has SAM ("single abstract method") only for Java interfaces."