If I have: ```interface Something { fun foo(bar...
# announcements
d
If I have:
Copy code
interface Something {
   fun foo(bar: Int): List<Int>
}

class SomethingImpl : Something {
   //...
}

val ref = Something::foo
and I want to invoke it, do I do this:
Copy code
val instance = SomethingImpl()
ref.invoke(instance, 5)
?
m
Wow, I didn't even know you could reference a method on an interface like that, and bind it to an implementation later.
s
You can rewrite
ref.invoke(instance, 5)
as
instance.ref(5)
.
d
Even if the
ref
might be null, I could still write
instance?.ref(5)
? It seems to turn out red in Intellij...
Even non-nullable seems to be red...
In my real use-case I have a list of pairs of object to funcRef
NamedGroups
.. and then I do:
Copy code
val namedGroup = (NamedGroups.firstOrNull { it.first.id === groupId }?.second)
Then I do:
Copy code
namedGroup?.invoke(storeRepository, user.storeId)
But it seems to be null for some reason...
Even though there is a match.
s
No, for that rewrite,
ref
can't be null. (
instance
can be null, though).
d
Thanks, now it makes sense!
By the way, maybe it's red because the rewrite doesn't work with
KSuspendFunction2
? Maybe suspend functions don't support this?
(the ref is a suspend fun in my code...)
s
Change the`ref` declaration: ​
val ref: Something.(Int) -> List<Int> = Something::foo
oops I forgot the (Int), it works now)... but it's funny how the same reference can be cast to both forms?
And that the default is the non-receiver form...
But thanks 😁!