# Add interface(trait?) without modifying a class

**URL:** <https://discuss.kotlinlang.org/t/add-interface-trait-without-modifying-a-class/9700>\
**Category:** Language Design\
**Created:** [October 5, 2018, 9:30am UTC](https://discuss.kotlinlang.org/t/add-interface-trait-without-modifying-a-class/9700 "2018-10-05T09:30:10Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![noo.blaster](https://avatars.discourse-cdn.com/v4/letter/n/ac91a4/32.png) [@noo.blaster](https://discuss.kotlinlang.org/u/noo.blaster)\
**Post date:** [October 5, 2018, 9:30am UTC](https://discuss.kotlinlang.org/t/add-interface-trait-without-modifying-a-class/9700/1 "2018-10-05T09:30:10Z")

</div>

it often happens that someone need to add a function on a type.

Kotlin extension are the way to go to solve this problem in general.

But sometimes, you want to add same functionality on different type you don’t have control onto, then pass them in another function that use thos functionality, ex:

```auto
// class I cannot modify
class ComingFromADependency1
class ComingFromADependency2

//my code
class MyType
trait MyTypeAble{
   fun toMyType: MyType
}

impl MyTypeAble for ComingFromADependency1{
  fun toMyType = ...
}

impl MyTypeAble for ComingFromADependency2{
  fun toMyType = ...
}

fun target(x: myTypeAble) {
// ...
  x.toMyType()
//...
}

```

This is a feature I love in rust. this is were I pick the word “trait”. Rust has a security forcing you to implement trait either in class package or in interface package (this avoid weird thing as you probably can imagine…).

now the problem is… java interroperability : I don’t see a resonable way to expose trait & use them in java… But maybe it is still possible. Maybe a solution would be not to allow to expose traits (trait would not be public then, confined in module).

But still because kotlin tends to be multiplatform, maybe it would be great to look beyond java interrop… But this is another topic.

what do you guys think about it ?

---

<div class="post-metadata">

**Author:** ![jstuyts](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/jstuyts/32/2141_2.png) [@jstuyts](https://discuss.kotlinlang.org/u/jstuyts)\
**Post date:** [October 5, 2018, 12:02pm UTC](https://discuss.kotlinlang.org/t/add-interface-trait-without-modifying-a-class/9700/2 "2018-10-05T12:02:14Z")

</div>

Use a function type instead of a trait as the parameter to `target`:

```auto
fun target(x: () -> MyType) {
// ...
     x()
// ...
}

```

---

<div class="post-metadata">

**Author:** ![noo.blaster](https://avatars.discourse-cdn.com/v4/letter/n/ac91a4/32.png) [@noo.blaster](https://discuss.kotlinlang.org/u/noo.blaster)\
**Post date:** [October 5, 2018, 4:08pm UTC](https://discuss.kotlinlang.org/t/add-interface-trait-without-modifying-a-class/9700/3 "2018-10-05T16:08:35Z")

</div>

Ok adding the function call…  
target(aComingFromADependency1)  
Or  
target(aComingFromADependency2)

---

<div class="post-metadata">

**Author:** ![jstuyts](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/jstuyts/32/2141_2.png) [@jstuyts](https://discuss.kotlinlang.org/u/jstuyts)\
**Post date:** [October 5, 2018, 4:51pm UTC](https://discuss.kotlinlang.org/t/add-interface-trait-without-modifying-a-class/9700/4 "2018-10-05T16:51:44Z")

</div>

Pass the body of the following function:

```auto
impl MyTypeAble for ComingFromADependency1{
  fun toMyType = ...
}

```

As a lambda:

```auto
// The dots here are what originally is supposed to be at the dots above.
target { … }

```

---

<div class="post-metadata">

**Author:** ![nwh](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/nwh/32/5138_2.png) [@nwh](https://discuss.kotlinlang.org/u/nwh)\
**Post date:** [October 6, 2018, 12:24am UTC](https://discuss.kotlinlang.org/t/add-interface-trait-without-modifying-a-class/9700/5 "2018-10-06T00:24:11Z")

</div>

I believe this KEEP issue is what you’re looking for: [Compile-time Extension Interfaces by raulraja · Pull Request #87 · Kotlin/KEEP · GitHub](https://github.com/Kotlin/KEEP/pull/87)

---

<div class="post-metadata">

**Author:** ![abrownvt](https://avatars.discourse-cdn.com/v4/letter/a/7ea924/32.png) [@abrownvt](https://discuss.kotlinlang.org/u/abrownvt)\
**Post date:** [October 9, 2018, 4:05pm UTC](https://discuss.kotlinlang.org/t/add-interface-trait-without-modifying-a-class/9700/6 "2018-10-09T16:05:39Z")

</div>

I also believe this is the KEEP OP is looking for, was going to share it until I saw someone else already did!

---

<div class="post-metadata">

**Author:** ![noo.blaster](https://avatars.discourse-cdn.com/v4/letter/n/ac91a4/32.png) [@noo.blaster](https://discuss.kotlinlang.org/u/noo.blaster)\
**Post date:** [October 14, 2018, 11:20am UTC](https://discuss.kotlinlang.org/t/add-interface-trait-without-modifying-a-class/9700/7 "2018-10-14T11:20:10Z")

</div>

Thx, that is close to what I’m trying to do.
