Kotlin allows you to extend concrete instances of a generically typed class. For example, suppose I have the following class Foo
, with extension functions Foo<Int>.bar()
and Foo<String>.bar()
:
class Foo<T>(t: T) {
val a: String = "bar"
val b: Int = 111
}
fun Foo<Int>.bar() = a
fun Foo<String>.bar() = b
fun main(args: Array<String>) {
val stringBar: String = Foo(1).bar()
val intBar: Int = Foo("").bar()
}
Is it possible to achieve this behavior without extension functions, and if so, how do I convert the extension functions into members? Is it possible without renaming or changing the type signatures?