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

PHondogo

09/09/2019, 8:15 AM
Hi! I have code like this:
Copy code
fun <T> test(v: T, lambda: (T) -> Unit ) {
  lambda(v)
}
And at call site i have code :
Copy 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?
r

rcd27

09/09/2019, 9:19 AM
What is
low bound
?
p

PHondogo

09/09/2019, 10:05 AM
I mean <in Parameter>
for example in java it would be:
Copy code
interface Test {
        <T> void test(T t, Consumer<? super T> consumer);
    }
r

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
Not sure if I've answered your question.