Here is a minimal example that demonstrates the problem:
abstract class Base {
abstract fun String.extension(x: Char)
}
class Derived : Base() {
override fun String.extension(x: Char) {
// Calling lots of methods on String, hence extension method
println("${first()} $length ${last()} ${firstOrNull { it == x }} ...")
}
}
Calling the extension method from Java is trivial:
Base o = new Derived();
o.extension("hello world", 'l');
But I can't figure out how to do it in pure Kotlin. Neither String
nor Base
seems to have the extension
method.
First, note that an extension function defined as a member requires two receivers, one is an instance of the enclosing class (dispatch receiver, usually this
of the enclosing class) and the other is the instance of the type that the function extends (extension receiver). This is documented here.
So, to call such a function from outside the class you have to provide two receivers. Kotlin doesn't have any syntax to do that explicitly like (x, "abc").stringExtension()
, but you can provide the dispatch receiver implicitly using an extension lambda:
class C(val name: String) {
fun String.extended() = this + " extended by " + name
}
fun main(args: Array<String>) {
val c = C("c")
with(c) { println("abc".extended()) }
}
(runnable demo of this code)
In the with(...) { ... }
block, c
becomes the implicit receiver, thus allowing you to use it as a dispatch receiver in C
member extensions. This would work with any other function that uses functional types with receivers: apply
, run
, use
etc.
In your case, it would be with(o) { "hello world".extension('l') }
As @KirillRakhman noted, an extension receiver of an extension function for C
can also be implicitly used as a dispatch receiver for the extensions defined inside C
:
fun C.someExtension() = "something".extended()
To decale an extension method outside the class using it, you should NOT implement it inside a class, and you should do like this:
package com.sample.test
import java.io.File
fun File.folderLength() : Long {
return 0L
}
So in your class that call this method:
package com.sample.util
import com.sample.test.*
import java.io.File
class foo{
fun getFolderSize(url: String) : Long{
var file = new File("...")
var length = file.folderLength()
return length
}
}
Hope this should help you.
Your extension function is defined only inside of Base/Derived class. See Declaring Extensions as Members.
abstract class Base {
abstract fun String.extension(x: Char)
}
class Derived : Base() {
override fun String.extension(x: Char) {
// Calling lots of methods on String, hence extension method
println("${first()} $length ${last()} ${firstOrNull { it == x }} ...")
}
fun callExtension(c: Char) {
"hello".extension(c)
}
}
fun main(args: Array<String>) {
val base = Derived()
base.callExtension('h')
}