# Contextual extension methods

**URL:** https://discuss.kotlinlang.org/t/contextual-extension-methods/5666
**Category:** Language Design
**Created:** [December 4, 2017, 11:42am UTC](https://discuss.kotlinlang.org/t/contextual-extension-methods/5666 "2017-12-04T11:42:35Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![scurab](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/scurab/32/4228_2.png) [@scurab](https://discuss.kotlinlang.org/u/scurab)
#### Post date: [December 4, 2017, 11:42am UTC](https://discuss.kotlinlang.org/t/contextual-extension-methods/5666/1 "2017-12-04T11:42:35Z")

</div>

Can I somehow define a “contextual” extension method as following example ?  
For case `SomeClass` is final and not part of my source code ?

```auto
class SomeClass {
    val value = true
    fun Boolean.toInt() = if (this == true) 1 else 0
}

fun func() {
    //true.toInt()//doesn't compile
    SomeClass().apply {
        value.toInt()//works fine
    }
}

```

Any ideas how to do it ?

---

<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 8, 2017, 10:26am UTC](https://discuss.kotlinlang.org/t/contextual-extension-methods/5666/2 "2017-12-08T10:26:23Z")

</div>

The `Boolean.toInt()` in your example becomes a member of `SomeClass`; it receives a `SomeClass` instance, can access its private members, etc. You cannot add a member to a class that is not part of your source code.

---

<div class="post-metadata">

### Author: ![scurab](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/scurab/32/4228_2.png) [@scurab](https://discuss.kotlinlang.org/u/scurab)
#### Post date: [December 13, 2017, 9:42am UTC](https://discuss.kotlinlang.org/t/contextual-extension-methods/5666/3 "2017-12-13T09:42:59Z")

</div>

Hi Yole,

thank you for your reply. I think you missed my point. I understand that adding a member to some non-own class is not possible. But this is purely about extension methods

```auto
class SomeClass {
    val value = true
    fun Boolean.toInt() = if (this == true) 1 else 0
}

fun Boolean.toInt2() = if (this == true) 1 else 0

fun func() {
    //true.toInt()//doesn't compile
    SomeClass().apply {
        value.toInt()//works fine
    }
}

```

As you can see in this example. Extension function `toInt2()` is visible everywhere.  
My point is to declare `toInt2()` somehow with same scope as `toInt()` has. So it would be accessible only in  
`SomeClass().apply { ... }`

Thing is, for some extension functions they don’t make much sense in global scope as they are tied to some specific type in terms of usage/meaning. So i was wondering if I could declare extension function scoped only to some `someObject.apply {}`

I believe technically it should be feasible as it’s nothing else than static java method with argument of extended type. In this case it would have 2 arguments, one the extended type and 2nd the object what I’m trying to scope with…

Thanks for any thoughts…

---

<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 13, 2017, 1:17pm UTC](https://discuss.kotlinlang.org/t/contextual-extension-methods/5666/4 "2017-12-13T13:17:40Z")

</div>

What you seem to want is the ability to have multiple receivers on a (extension) function. Currently this is not possible even though there appear to be (limited) use cases for it. Note that even the member extension functions can be quite awkward to use.
