# "Useless" member extensions

**URL:** <https://discuss.kotlinlang.org/t/useless-member-extensions/2151>\
**Category:** Uncategorized\
**Created:** [December 15, 2016, 12:58pm UTC](https://discuss.kotlinlang.org/t/useless-member-extensions/2151 "2016-12-15T12:58:14Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![fab1an](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/fab1an/32/1934_2.png) [@fab1an](https://discuss.kotlinlang.org/u/fab1an)\
**Post date:** [December 15, 2016, 12:58pm UTC](https://discuss.kotlinlang.org/t/useless-member-extensions/2151/1 "2016-12-15T12:58:14Z")

</div>

I can define member extensions like this, but is there a way to use it?

```
interface Section<T> {
    fun matches(item: T): Boolean
}

class MyList<T>{
    val items: List<T> = listOf()
    
    val Section<T>.firstMatch: Int
        get() = items.indexOfFirst { this.matches(it) }
}

fun myMethod() {
    val myList = MyList<String>()
        
    val section = object : Section<String> {
        override fun matches(item: String): Boolean = 
            item == "something"
    }
 
    
    /* ??? */
    val x = section.firstMatch 
}

```

---

<div class="post-metadata">

**Author:** ![yole](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/yole/32/1425_2.png) [@yole](https://discuss.kotlinlang.org/u/yole)\
**Post date:** [December 15, 2016, 1:17pm UTC](https://discuss.kotlinlang.org/t/useless-member-extensions/2151/2 "2016-12-15T13:17:01Z")

</div>

`val x = with(myList) { section.firstMatch }`

---

<div class="post-metadata">

**Author:** ![fab1an](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/fab1an/32/1934_2.png) [@fab1an](https://discuss.kotlinlang.org/u/fab1an)\
**Post date:** [December 15, 2016, 1:42pm UTC](https://discuss.kotlinlang.org/t/useless-member-extensions/2151/3 "2016-12-15T13:42:03Z")

</div>

Thank you, that wasn’t obvious to me from the documentation.

---

<div class="post-metadata">

**Author:** ![fab1an](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/fab1an/32/1934_2.png) [@fab1an](https://discuss.kotlinlang.org/u/fab1an)\
**Post date:** [December 15, 2016, 1:59pm UTC](https://discuss.kotlinlang.org/t/useless-member-extensions/2151/4 "2016-12-15T13:59:46Z")

</div>

And is there a way to write that extension function in `Section` so that `MyList` doesn’t need to know about it?

---

<div class="post-metadata">

**Author:** ![pdvrieze](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/pdvrieze/32/1882_2.png) [@pdvrieze](https://discuss.kotlinlang.org/u/pdvrieze)\
**Post date:** [December 16, 2016, 11:57am UTC](https://discuss.kotlinlang.org/t/useless-member-extensions/2151/5 "2016-12-16T11:57:05Z")

</div>

The reason for this kind of extension function (that effectively takes 2 this pointers) is to access both. You could of course reverse it and put it in Section instead. In some cases you need to use the `this@...` syntax though.
