Hello, Word! I was wondering if there's any way to...
# announcements
b
Hello, Word! I was wondering if there's any way to craft a Java method in such a way that it can be called in Kotlin like a "function literals with receiver". So I could call
myObj.foo { bar() }
where
bar
is a method on some class passed as the receiver of the lambda - but
foo
is declared/implemented in Java. Ideas? 🙂
a
I don't think that is easily possible, why do you need that?
b
it's just that I make a Java library, but I want it to also be Kotlin friendly
t
wouldn't it be possible to write in Kotlin? As Java users can still call those methods
receiver is a concept in Kotlin, even if it's possible, you'll probably need some Kotlin annotation from stdlib, so you might as well declare the surface API in Kotlin
b
hmm that's true but then I need to make it depend on the Kotlin stdlib - and since it's a small library that's a bit "too much"
t
you can try to see what metadata annotations are attached when declaring a lambda with receiver in the class files and mimic that from Java, I never tried to hack around with this yet.
javap
will be your friend
b
I guess I could just add extensions to a kotlin specific optional dependency.... 😛
t
yeah, that's what everyone does 🙂
be careful with overloading a member from an extension; member always wins; if it doesn't work you may need to introduce a new name for kt
b
oh thanks for the tip! 🙂
t