# Anonymous class which extends interface, and/or correct way to do this in Kotlin

**URL:** <https://discuss.kotlinlang.org/t/anonymous-class-which-extends-interface-and-or-correct-way-to-do-this-in-kotlin/1173>\
**Category:** Uncategorized\
**Created:** [May 17, 2012, 5:46pm UTC](https://discuss.kotlinlang.org/t/anonymous-class-which-extends-interface-and-or-correct-way-to-do-this-in-kotlin/1173 "2012-05-17T17:46:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![vonwao](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/vonwao/32/1448_2.png) [@vonwao](https://discuss.kotlinlang.org/u/vonwao)\
**Post date:** [May 17, 2012, 5:46pm UTC](https://discuss.kotlinlang.org/t/anonymous-class-which-extends-interface-and-or-correct-way-to-do-this-in-kotlin/1173/1 "2012-05-17T17:46:23Z")

</div>

This probably is in the docs somewhere if I search carefully, but how do I do this in Kotlin (since Runnable is an interface and doesn't have a constructor, I think Kotlin doesn't like this):

&nbsp;&nbsp;private fun updateResultView(msg : String) : Unit { &nbsp;&nbsp;runOnUiThread(object : Runnable() { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;public override fun run() : Unit { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;resultView?.setText((msg)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;} &nbsp;&nbsp;}) &nbsp;&nbsp;}

Also, I know this is very ugly code in Kotlin, can someone remind me what the right way to do this is?&nbsp;&nbsp;(runOnUiThread is a method in Android Activity superclass).&nbsp;&nbsp;What I really want to do is create Kotlin extension function that I can reuse in ANY activity class.&nbsp;&nbsp;

The Java-Kotlin code converter works awesome!&nbsp;&nbsp;This is the one issue I found with it (and I wanted to get started quickly and just convert some code to Kotlin, and then refactor it slowly as I go.)

---

<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:** [May 17, 2012, 9:20pm UTC](https://discuss.kotlinlang.org/t/anonymous-class-which-extends-interface-and-or-correct-way-to-do-this-in-kotlin/1173/2 "2012-05-17T21:20:45Z")

</div>

As of compilation error: simply remove the parentheses after Runnable.

To do this code more nicely you might want to get rid of the Runnable by introducing a funciton that converts a funciton to a Runnable:

```kotlin
fun r(f: () -> Unit) : Runnable = object : Runnable {
    public override fun run() {
        f()
    }
}
```

And now you'd say

```kotlin

    private fun updateResultView(msg : String) : Unit {
        runOnUiThread(r {
           resultView?.setText(msg)
        })
    }
```

Or, better do runOnUiThread() as an extension function:

```kotlin
    fun Activity.runOnUiThread(f: () -> Unit) {
        runOnUiThread(object : Runnable() {
           public override fun run() : Unit {
           f()
           }
        })
    }
```

And then it will simply be

```kotlin
funOnUiThread {
    resultView?.setText(msg)
} 
```

If you encounter a problem with the Java2Kotlin converter, please report it to our tracker. Thanks!

---

<div class="post-metadata">

**Author:** ![vonwao](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/vonwao/32/1448_2.png) [@vonwao](https://discuss.kotlinlang.org/u/vonwao)\
**Post date:** [May 18, 2012, 2:13pm UTC](https://discuss.kotlinlang.org/t/anonymous-class-which-extends-interface-and-or-correct-way-to-do-this-in-kotlin/1173/3 "2012-05-18T14:13:16Z")

</div>

Thanks so much, Andrey.&nbsp;&nbsp;This Activity.runOnUiThread is exactly what I was looking for :)&nbsp;&nbsp;but couldn't figure out how to write it, I guess I'm still not very good "thinking in Kotlin", higher order functions, etc.

By the way, is there any thoughts for adding kotlin.android package with these sort of extension functions (I can think of several extension functions already that would be useful).&nbsp;&nbsp;Would you accept contributions in this area?

I was searching and I see that there is already a kotlin.concurrent package that has some TimerTask and thread functions that would be useful in Android, but there is also other stuff in Android, such as Handlers and of course all the UI stuff.

---

<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:** [May 18, 2012, 11:44pm UTC](https://discuss.kotlinlang.org/t/anonymous-class-which-extends-interface-and-or-correct-way-to-do-this-in-kotlin/1173/4 "2012-05-18T23:44:31Z")

</div>

We have just strated working on the android support: porting SDK examples to Kotlin and fixing many misterious bugs. I think, the time is perfect for such contributions as Android-specific extension functions. Please, feel free to create a kotlin-android directory under the libraries: [https://github.com/JetBrains/kotlin/tree/master/libraries](https://github.com/JetBrains/kotlin/tree/master/libraries), and put your extensions there. Thanks.
