Is it possible to make an extention-function that ...
# announcements
k
Is it possible to make an extention-function that is only callable in the context off a certain type with certain type parameters?
o
you can do something like gradle does, and instead of generic
run
define either
invoke
or an actual function like
fun Context<Int>.xyz(block: YourScope.() -> Unit)
. Then define
YourScope
with the appropriate extension function.
k
Not ideal, but I guess that would work for now.
s
how about passing foo as a parameter to
test
? In that case this works:
Copy code
fun main() {
    val foo: String = ""

    Context<Int>(21).run{
        test(foo) // should compile
    }

    Context<String>("test").run{
        test(foo) // shouldn't compile
    }
}

class Context<T>(value: T) {
}

fun Context<Int>.test(value: String) {
    //
}