# Extension functions usage

**URL:** <https://discuss.kotlinlang.org/t/extension-functions-usage/1247>\
**Category:** Uncategorized\
**Created:** [March 17, 2012, 7:01pm UTC](https://discuss.kotlinlang.org/t/extension-functions-usage/1247 "2012-03-17T19:01:14Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![fedos.serg](https://avatars.discourse-cdn.com/v4/letter/f/f1d935/32.png) [@fedos.serg](https://discuss.kotlinlang.org/u/fedos.serg)\
**Post date:** [March 17, 2012, 7:01pm UTC](https://discuss.kotlinlang.org/t/extension-functions-usage/1247/1 "2012-03-17T19:01:14Z")

</div>

Hello, I noticed that extension functions are widely used in Kotlin default library i.e. Kotlin String class contains just few basic methods, but a lot of other methods are implemented as extension functions. I thought that extension functions were ment to be helpers for those who don't have access to source but still want nice helper methods for some classes. I can see only one reason for this usage of extension function - to reduce number of autocomplete options when some packages are not imported. But as for me it makes usage of such classes too complicated. Can you tell about the idea of extension functions some more details if there are those?

I also have a problem with this particular case:

``

```kotlin
package test;
import std.io.println
import kotlin.*
fun main(args : Array<String>) {
  println(“test”.matches("."))
}
```

  
_this code is not compiled as matches() extension functions cannot be found. I thought there is an issue with imports and I search for an example in Kotlin sources. I found some and added import kotlin._ but it did help. There was also a kotlin.util.\* import there but it can not be resolved in my case.  
Thanks in advance.

---

<div class="post-metadata">

**Author:** ![Frostman](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/frostman/32/1617_2.png) [@Frostman](https://discuss.kotlinlang.org/u/Frostman)\
**Post date:** [March 17, 2012, 7:07pm UTC](https://discuss.kotlinlang.org/t/extension-functions-usage/1247/2 "2012-03-17T19:07:47Z")

</div>

What version of Kotlin plugin and runtime do you using?

It works at the latest build from sources.

---

<div class="post-metadata">

**Author:** ![fedos.serg](https://avatars.discourse-cdn.com/v4/letter/f/f1d935/32.png) [@fedos.serg](https://discuss.kotlinlang.org/u/fedos.serg)\
**Post date:** [March 17, 2012, 7:19pm UTC](https://discuss.kotlinlang.org/t/extension-functions-usage/1247/3 "2012-03-17T19:19:41Z")

</div>

I use plugin 0.1.1844 and runtime comes with it. It was the last stable build for today.

---

<div class="post-metadata">

**Author:** ![lacroix1547](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/lacroix1547/32/1567_2.png) [@lacroix1547](https://discuss.kotlinlang.org/u/lacroix1547)\
**Post date:** [March 17, 2012, 7:48pm UTC](https://discuss.kotlinlang.org/t/extension-functions-usage/1247/4 "2012-03-17T19:48:20Z")

</div>

Dont you think that extension functions are a nice way to augment the jdk ?

---

<div class="post-metadata">

**Author:** ![fedos.serg](https://avatars.discourse-cdn.com/v4/letter/f/f1d935/32.png) [@fedos.serg](https://discuss.kotlinlang.org/u/fedos.serg)\
**Post date:** [March 17, 2012, 7:53pm UTC](https://discuss.kotlinlang.org/t/extension-functions-usage/1247/5 "2012-03-17T19:53:50Z")

</div>

Yes they are and not only JDK but all the other libraries. But what I mean here is that they are used for example to imlement matches() method in Kotlin String class. And this usage is not obvious for me.

---

<div class="post-metadata">

**Author:** ![lacroix1547](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/lacroix1547/32/1567_2.png) [@lacroix1547](https://discuss.kotlinlang.org/u/lacroix1547)\
**Post date:** [March 17, 2012, 8:08pm UTC](https://discuss.kotlinlang.org/t/extension-functions-usage/1247/6 "2012-03-17T20:08:38Z")

</div>

Do you mean that &nbsp;&nbsp;inline fun String.matches(regex : String) = (this as java.lang.String).matches(regex).sure() is not obvious ?

---

<div class="post-metadata">

**Author:** ![fedos.serg](https://avatars.discourse-cdn.com/v4/letter/f/f1d935/32.png) [@fedos.serg](https://discuss.kotlinlang.org/u/fedos.serg)\
**Post date:** [March 17, 2012, 8:15pm UTC](https://discuss.kotlinlang.org/t/extension-functions-usage/1247/7 "2012-03-17T20:15:28Z")

</div>

The usage of extension function is not obvious in this case. String here is a Kotlin class so the same method can be implemented inside it so as others from String.kt.

---

<div class="post-metadata">

**Author:** ![lacroix1547](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/lacroix1547/32/1567_2.png) [@lacroix1547](https://discuss.kotlinlang.org/u/lacroix1547)\
**Post date:** [March 17, 2012, 8:18pm UTC](https://discuss.kotlinlang.org/t/extension-functions-usage/1247/8 "2012-03-17T20:18:07Z")

</div>

So what could be a better alternative ?

---

<div class="post-metadata">

**Author:** ![fedos.serg](https://avatars.discourse-cdn.com/v4/letter/f/f1d935/32.png) [@fedos.serg](https://discuss.kotlinlang.org/u/fedos.serg)\
**Post date:** [March 17, 2012, 8:29pm UTC](https://discuss.kotlinlang.org/t/extension-functions-usage/1247/9 "2012-03-17T20:29:07Z")

</div>

> String here is a Kotlin class so the same method can be implemented inside it so as others from String.kt.

---

<div class="post-metadata">

**Author:** ![cedricbeust](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/cedricbeust/32/1429_2.png) [@cedricbeust](https://discuss.kotlinlang.org/u/cedricbeust)\
**Post date:** [March 17, 2012, 9:31pm UTC](https://discuss.kotlinlang.org/t/extension-functions-usage/1247/10 "2012-03-17T21:31:15Z")

</div>

I'm guessing the question asked here is more one of good practice: when you have the source of the class you are trying to augment, when should you just add your method there instead of using an extension method?

---

<div class="post-metadata">

**Author:** ![lacroix1547](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/lacroix1547/32/1567_2.png) [@lacroix1547](https://discuss.kotlinlang.org/u/lacroix1547)\
**Post date:** [March 17, 2012, 10:08pm UTC](https://discuss.kotlinlang.org/t/extension-functions-usage/1247/11 "2012-03-17T22:08:02Z")

</div>

Ok got it. I wonder if the bytecode output will be 100% the same with both methods.

---

<div class="post-metadata">

**Author:** ![fedos.serg](https://avatars.discourse-cdn.com/v4/letter/f/f1d935/32.png) [@fedos.serg](https://discuss.kotlinlang.org/u/fedos.serg)\
**Post date:** [March 18, 2012, 6:45am UTC](https://discuss.kotlinlang.org/t/extension-functions-usage/1247/12 "2012-03-18T06:45:08Z")

</div>

It defenetely is not: Class functions generate common methods inside classes. Extension functions generate outside static method with additional reciever parameter.

---

<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:** [March 18, 2012, 7:03am UTC](https://discuss.kotlinlang.org/t/extension-functions-usage/1247/13 "2012-03-18T07:03:36Z")

</div>

It's good to keep APIs of your classes minimal. With extension functions you can have all the convenience of a rich API, but having only necessary things actually exposed from your class. One other thing is that if you don't like the set of extensions Kotlin provides today (e.g. funciton names are confusing or soething else bothers you), you can skip those and use some other set of extensions to String.
