# Extensions function to implement interface

**URL:** <https://discuss.kotlinlang.org/t/extensions-function-to-implement-interface/411>\
**Category:** Uncategorized\
**Created:** [November 13, 2014, 9:29pm UTC](https://discuss.kotlinlang.org/t/extensions-function-to-implement-interface/411 "2014-11-13T21:29:08Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![msgile](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/msgile/32/1484_2.png) [@msgile](https://discuss.kotlinlang.org/u/msgile)\
**Post date:** [November 13, 2014, 9:29pm UTC](https://discuss.kotlinlang.org/t/extensions-function-to-implement-interface/411/1 "2014-11-13T21:29:08Z")

</div>

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.&nbsp;&nbsp;The syntax for this could be (taken strait from Swift)

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

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

  }
  }
}
```

---

<div class="post-metadata">

**Author:** ![abreslav](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/abreslav/32/8279_2.png) [@abreslav](https://discuss.kotlinlang.org/u/abreslav)\
**Post date:** [November 14, 2014, 10:04am UTC](https://discuss.kotlinlang.org/t/extensions-function-to-implement-interface/411/2 "2014-11-14T10:04:25Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![banxi1988](https://avatars.discourse-cdn.com/v4/letter/b/a3d4f5/32.png) [@banxi1988](https://discuss.kotlinlang.org/u/banxi1988)\
**Post date:** [October 12, 2017, 1:20pm UTC](https://discuss.kotlinlang.org/t/extensions-function-to-implement-interface/411/3 "2017-10-12T13:20:04Z")

</div>

hope we will have this feature some day

---

<div class="post-metadata">

**Author:** ![christopheblin](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/christopheblin/32/5890_2.png) [@christopheblin](https://discuss.kotlinlang.org/u/christopheblin)\
**Post date:** [December 21, 2018, 8:20am UTC](https://discuss.kotlinlang.org/t/extensions-function-to-implement-interface/411/4 "2018-12-21T08:20:12Z")

</div>

I post here because I’ve found this on google but this did not give me the answer I was looking for …

With kotlin you can directly do something like this :

```
fun String.asTransformable() = object:Transformable {
  override fun <T> transformTo(type: Class<T>): T = with(this@asTransformable) {
    when (type) { ... }
  }
}

```

the key here is the anonymous implementation and the with(this@asTransformable) which allows to get access to the instance nearly as normally as you could expect

---

<div class="post-metadata">

**Author:** ![DonizeteVida](https://avatars.discourse-cdn.com/v4/letter/d/3ec8ea/32.png) [@DonizeteVida](https://discuss.kotlinlang.org/u/DonizeteVida)\
**Post date:** [June 6, 2022, 2:40am UTC](https://discuss.kotlinlang.org/t/extensions-function-to-implement-interface/411/5 "2022-06-06T02:40:20Z")

</div>

But you can’t do something like this:

```auto
suspend fun <T> ListenableFuture<T>.suspendDisposable(): T where T : Disposable =
    suspendCancellableCoroutine {
        it.invokeOnCancellation {
            if (!isDone) {
                cancel(true)
                return@invokeOnCancellation
            }
            val result = get()
            result.dispose()
        }
        addListener(
            {
                it.resume(get())
            },
            BlockingExecutor
        )
    }

```
