Why do we use invoke operator overloading in Kotlin Usecases?

Viewed 285

I've started using use-cases in kotlin for clean coding practices but not clear why we use the invoke operator overloading. What's the point and why not call it execute() instead?

interface GetUserUseCase {
    operator fun invoke(): Single<Result<User>>
}
1 Answers

Use invoke operator on UseCase provides some benefits:

  • Enforces a pseudo-contract on a contract-less UseCase
  • Reads more naturally
  • Flexibility in invocation ( loginUseCase() vs loginUseCase.invoke() )
Related