title: Kotlin recommended way of unregistering a listener with a SAM
body: <p>So i have an interactor which performs an insert operation with Realm and then notifies that the insertion is completed with a RealChangeListener. It is something like this:</p>
  
  <pre><code>fun insertCar(item: Car) {
          realm.doInTransaction {
              val car = Car(...)
              val copy = copyToRealm(car)
              copy.addChangeListener(...)
          }
      }
  </code></pre>
  
  <p>I can do it this way:</p>
  
  <pre><code>fun insertCar(item: Car, listener: RealmChangeListener&lt;Car&gt;) {
              realm.doInTransaction {
                  val car = Car(...)
                  val copy = copyToRealm(car)
                  copy.addChangeListener(listener)
              }
          }
  </code></pre>
  
  <p>And access like this:</p>
  
  <pre><code>realmInteractor.insertCar(item, RealmChangeListener {
     // do something here
  })
  </code></pre>
  
  <p>But then i have no way of removing this listener</p>
  
  <pre><code>realmInteractor.insertCar(item, RealmChangeListener {
     // do something here
     it.removeChangeListener(this)
  })
  </code></pre>
  
  <p><code>this</code> will point to the class its located not the actual listener</p>
  
  <p>I can also do this:</p>
  
  <pre><code>fun insertCar(item: Car, doAfterChange (Car) -&gt; Unit) {
                  realm.doInTransaction {
                      val car = Car(...)
                      val copy = copyToRealm(car)
                      copy.addChangeListener(RealmChangeListener{
                      doAfterChange()
                      })
                  }
              }
  </code></pre>
  
  <p>But then i have a SAM inside another SAM (too overkill imo)</p>
  
  <p>I can do it like this:</p>
  
  <pre><code>fun insertCar(item: Car, listener: RealmChangeListener&lt;Car&gt;) {
                  realm.doInTransaction {
                      val car = Car(...)
                      val copy = copyToRealm(car)
                      copy.addChangeListener(listener)
                  }
              }
  
  realmInteractor.insertCar(item, object : RealmChangeListener&lt;Car&gt; {
                          override fun onChange(element: Car?) {
                              ...
                              element?.removeChangeListener(this)
                          }
                      })
  </code></pre>
  
  <p>Which works but it is too verbose.</p>
  
  <p>So how do you deal with this and what is considered the best approach?</p>