-2

I have an interface with two methods:

interface Human {
   fun talk()
   fun think()
}

I want to pass an anonymous instance of this interface into a method.

How do I do this?

nz_21
  • 6,140
  • 7
  • 34
  • 80
  • https://kotlinlang.org/docs/reference/nested-classes.html#anonymous-inner-classes – JB Nizet Aug 07 '19 at 19:42
  • The answer to this one has an example : https://stackoverflow.com/questions/37672023/how-to-create-an-instance-of-anonymous-interface-in-kotlin – racraman Aug 07 '19 at 20:11

1 Answers1

1

If foo looks like this:

fun foo(human: Human) {
    // ...
}

You can invoke it like this, using an object expression:

foo(object: Human {
    override fun think() {
        // ...
    }

    override fun talk() {
        //...
    }
})
Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121