Here's a different way of explaining it that might help.
If you want to assign an implementation of an interface to a variable or property, you usually have to use an anonymous class syntax like this. Note the use of object: and override.
val onWebResponseHandler = object : VolleyHandler.WebResponseHandler { override fun onWebResponseFinished(jsonObject: JSONObject) { Log.v("da", jsonObject.toString()) } }
You could also define other public functions in your anonymous class, but this is pointless because there is no way to access the unique members of an anonymous class, so you would not be able to call the function foo() in this example:
val onWebResponseHandler = object : VolleyHandler.WebResponseHandler { override fun onWebResponseFinished(jsonObject: JSONObject) { Log.v("da", jsonObject.toString()) } fun foo() { Log.v("something", "something") } }
If you make your interface a fun interface, that enables the use of lambda syntax to define that single function like this. This is what SAM conversion is. It treats your lambda as an anonymous implementation of your interface.
val onWebResponseHandler = VolleyHandler.WebResponseHandler { jsonObject -> Log.v("da", jsonObject.toString()) }
Furthermore, if a lambda has only one parameter, you can omit defining the parameter name, and simply call it it so this is equivalent to the above:
val onWebResponseHandler = VolleyHandler.WebResponseHandler { Log.v("da", it.toString()) }
Also, be aware that you can define a function anywhere in your code, including right in the middle of another function. For example:
fun bar() { fun foo() { Log.i("foo", "foo") } foo() }
is equivalent to
fun bar() { val foo: () -> Unit = { Log.i("foo", "foo") } foo() // or foo.invoke() }
So in your original code, you combined these concepts to create a lambda function that has other functions defined in it that are never used. The confusion probably comes because it looks similar to my top block of code, where we are defining an anonymous class with the object: syntax. The big difference is that you omitted object: and override, so you are using the lambda syntax. Your code is equivalent to:
val onWebResponseHandler = VolleyHandler.WebResponseHandler { jsonObject -> val onWebResponseFinished: (JSONObject) -> Unit = { Log.v("da", "dsa") } val foo: () -> Unit = { jsonObject.getString("name") } }
which without lambda syntax (SAM conversion) would look like
val onWebResponseHandler = object : VolleyHandler.WebResponseHandler { override fun onWebResponseFinished(jsonObject: JSONObject) { val onWebResponseFinished: (JSONObject) -> Unit = { Log.v("da", "dsa") } val foo: () -> Unit = { jsonObject.getString("name") } } }
object :after the=sign:= object : VolleyHandler.WebResponseHandler() { ... }