Call super method from delegate


#1

Hi!
So I was wondering, is there any way to call super method from delegate?
I tried to do something like this, but this isn’t working

 class Test : List<String> by ArrayList(){

  override fun get(index: Int): String {
       println("Get method")
       return super<List>.get(index)
   }

}

#2

You can do this fairly easily but you have to capture the delegate explicitly.
Something like this should work:

class Test(private val delegate: List<String> = ArrayList()): List<String> by delegate {
    override fun get(index: Int): String {
        println("Get method")
        return delegate.get(index)
    }
}

You were delegating to an anonymous instance of an ArrayList so you couldn’t access it in your override.