Extensions function to implement interface


#1

Apples new Language Swift looks a lot like Kotlin and has a feature that would be really useful in Kotlin: The ability to extend a class to support an interface by adding extension methods.  The syntax for this could be (taken strait from Swift)

trait Transformable {   fun <T> transformTo(type: Class<T>):T }

extension String: Transformable {   override fun <T> transformTo(type: Class<T>): T{   when(type) {   javaClass<Boolean>() -> this.startsWith("T");   ...

  }
  }
}


"Extension types" for Kotlin
#2

Yes, it's a nice feature, not very straightforward to implement on the JVM though: it requires either dynamic method resolution (invokedynamic might help, but then we'd need to limit the scope of calls that are bound by invokedynamic, so it can't be uniform), or wrapping (this plainly breaks identity and equality).


#3

hope we will have this feature some day