I have a third party Java library which an object with interface like this:
public interface Handler {
void call(C context) throws Exception;
}
>
Assuming the interface has only a single method you can make use of SAM
val handler = Handler { println("Hello: $it") }
If you have a method that accepts a handler then you can even omit type arguments:
fun acceptHandler(handler:Handler){}
acceptHandler(Handler { println("Hello: $it") })
acceptHandler({ println("Hello: $it") })
acceptHandler { println("Hello: $it") }
If the interface has more than one method the syntax is a bit more verbose:
val handler = object: Handler2 {
override fun call(context: String?) { println("Call: $context") }
override fun run(context: String?) { println("Run: $context") }
}