# Passing reference to function

**URL:** <https://discuss.kotlinlang.org/t/passing-reference-to-function/1358>\
**Category:** Support\
**Created:** [December 14, 2015, 1:20pm UTC](https://discuss.kotlinlang.org/t/passing-reference-to-function/1358 "2015-12-14T13:20:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jvskriubakken](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/jvskriubakken/32/1430_2.png) [@jvskriubakken](https://discuss.kotlinlang.org/u/jvskriubakken)\
**Post date:** [December 14, 2015, 1:20pm UTC](https://discuss.kotlinlang.org/t/passing-reference-to-function/1358/1 "2015-12-14T13:20:49Z")

</div>

I’m having something “observable” like this:

```
class SomethingObservable {

   public fun onSomething(listener: (event: Event) -> Unit) {
      // adds listener 
   }

   public fun unSomething(listener: (event: Event) -> Unit) {
      // removes listener 
   }
}

```

And I would like to pass a class member function as the function reference like this:

```
class SomeoneObserving {
  
     val somethingObservable: SomethingObservable
    
     fun startObserving() {
        somethingObservable.onSomething(::handleEvent)
     }

     fun stopObserving() {
        somethingObservable.unSomething(::handleEvent)
     }

     private fun handleEvent(event: Event) {
          ...
     }
}

```

But instead I get the compilation errors:

- `Type mismatch: inferred type is kotlin.reflect.KFunction2<SomeoneObserving, Event, kotlin.Unit> but (Event) -> kotlin.Unit was expected`
- `Left-hand side of a callable reference with a receiver parameter cannot be empty. Please specify the type of the receiver before '::' explicitly`

Does anyone know how to accomplish this?

Thanks!

Jørund

---

<div class="post-metadata">

**Author:** ![pniederw1](https://avatars.discourse-cdn.com/v4/letter/p/0ea827/32.png) [@pniederw1](https://discuss.kotlinlang.org/u/pniederw1)\
**Post date:** [December 14, 2015, 3:42pm UTC](https://discuss.kotlinlang.org/t/passing-reference-to-function/1358/2 "2015-12-14T15:42:50Z")

</div>

As far as I know, function references are currently quite limited, and you’ll have to use `somethingObservable.onSomething { handleEvent(it) }` for now.

---

<div class="post-metadata">

**Author:** ![ilya.gorbunov](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/ilya.gorbunov/32/1645_2.png) [@ilya.gorbunov](https://discuss.kotlinlang.org/u/ilya.gorbunov)\
**Post date:** [December 14, 2015, 11:11pm UTC](https://discuss.kotlinlang.org/t/passing-reference-to-function/1358/3 "2015-12-14T23:11:05Z")

</div>

Better not to pass listener as `{ handleEvent(it) }` if you want to unsubscribe that listener later. The point is that each lambda expression corresponds to its own instance of the function `(event: Event) -> Unit`.

It would be better for now to store the function in a property, and pass value of that property as listener:

```auto
class SomeoneObserving {
  
     val somethingObservable: SomethingObservable
    
     fun startObserving() {
        somethingObservable.onSomething(handleEvent)
     }

     fun stopObserving() {
        somethingObservable.unSomething(handleEvent)
     }

     private val handleEvent = { event: Event ->
          ...
     }
}

```
