what does it mean that kpropertyx interface extends an expression like – v?

Making my own example here :

class Foo : () -> String {

        override fun invoke(): String {
        return "Bar"
        }   
    }

Which allows you to do:

 val result = Foo().invoke()

or, as pointed in the comments, you can invoke it straight by doing

Foo()().

So, after doing more research, I’ve found out that the invoke method is an operator function which, in this case, is also part of the lambda function (or any function), so to write the equivalent thing using an interface, you have to add the operator modifier

Something like the following :

   interface Bar {
    operator fun invoke(): String
}

class Foobar : Bar {

    override operator fun invoke(): String {
        return "foo"
    }
}

Essentially, this is just defining what happens when this method is being invoked

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top