0

I have the following interface (SAM):

  fun interface  WebResponseHandler
{
    fun onWebResponseFinished(jsonString:String?)
}

and Inside a class, (lets call the class ClassA) i wrote the following code:

 private val onInitWebResponseHandler: VolleyHandler.WebResponseHandler = VolleyHandler.WebResponseHandler()
{
    Thread(ParseJsonStringOnInit(WeakReference(this),
            weakRefIOnAllScoresDataFirstFetched, it)).start()

}

i can't understand why in the first argument of ParseJsonStringOnInit, this is considered as ClassA and not as VolleyHandler.WebResponseHandler? Is this a convention when using SAM?

Eitanos30
  • 1,331
  • 11
  • 19
  • Why would you expect that? after all this is just a syntactic sugar for creating a class that implements an interface and has nothing to do with SAM. – Tomer Arazy Jan 09 '21 at 17:22
  • Lambdas don't have a `this` unless there is a receiver parameter. If you need to refer to the anonymous class, you can't use the lambda syntax. – Tenfour04 Jan 09 '21 at 17:46
  • So to what `this` refer in this situation? Since code compiles and according to what i see (not sure) it refer to the class that contains this piece of code – Eitanos30 Jan 09 '21 at 18:22
  • 1
    It refers to the nearest valid containing scope, which would be the class this property is defined in. You can ctrl+click it and the IDE will jump the cursor to what it refers to. – Tenfour04 Jan 09 '21 at 18:25
  • @Tenfour04, in SAM, the lambda expression will never have a receiver. Am i right? – Eitanos30 Jan 09 '21 at 18:38
  • I can't think of how it could ever have a receiver. – Tenfour04 Jan 09 '21 at 18:39

0 Answers0