PHondogo
09/09/2019, 8:15 AMfun <T> test(v: T, lambda: (T) -> Unit ) {
lambda(v)
}
And at call site i have code :
val lambda = {v : Any -> println(v)}
test<SomeObject>(SomeObject(), lambda)
The code below wont compile, cause compiler want to have my val lambda have parameter type exact as declared in function.
How should I modify my fun test declaration to allow <in Parameter> in lambda parameter?rcd27
09/09/2019, 9:19 AMlow bound
?PHondogo
09/09/2019, 10:05 AMinterface Test {
<T> void test(T t, Consumer<? super T> consumer);
}
rcd27
09/09/2019, 11:34 AM(T) -> R
means a function, which accepts T
and returns R
. By this meaning, T
is always in
, so basically this is the same as Function
interface in Java: it accepts one thing and returns one thing. In the example you provided Consumer
accepts T
and returns nothing. In Kotlin it would be (T) -> Unit
, so as you wrote