You are currently viewing Is there any reason to prefer `operator fun invoke()` rather than implementing `() -> T` as an interface?

Is there any reason to prefer `operator fun invoke()` rather than implementing `() -> T` as an interface?

To make an object instance callable, we have two options: write an operator overload for invoke() or implement a function interface. E.g.,

fun callThenPrint(block: () -> Int) { println(block()) } val o1 = object : () -> Int { override fun invoke() = 1 } println(o1()) callThenPrint(o1) val o2 = object { operator fun invoke() = 2 } println(o2()) callThenPrint(o2::invoke) 

As we can see, from the only two usage scenarios my feeble brain can imagine, it seems like the interface approach is superior, all other things equal.

The only reason we might ever need the operator version is because Kotlin doesn’t allow us to declare interface adherence post-definition, so you could use an extension function on a type you don’t control to add the invoke operator.

That is, unless I’m missing something. Am I?

EDIT: Bonus points: Does the operator word actually do anything if I’m already implementing the interface? E.g.,

val o1 = object : () -> Int { override **operator** fun invoke() = 1 } 

submitted by /u/ragnese
[link] [comments]