Rgfvfk Iff
Rgfvfk Iff

Reputation: 1609

Kotlin lambdas without using objects

I am trying to convert from java to kotlin. The current java interface is something like this:

interface MyInterface {
    void foo(int x, int y);
}

MyInterface testing = (int a, int b) -> System.out.print("TESTING");

My current kotlin conversion is:

interface MyInterface {
    fun foo(x:Int, y:Int)
}

val kotlinConversion = object: MyInterface {
    override fun foo(x: Int, y: Int) {
        println("TESTING")
    }
}

Is there a way to write the variable kotlinConversion such that it is similar to the one in java without having to override the function?

Upvotes: 1

Views: 64

Answers (1)

Sasi Kumar
Sasi Kumar

Reputation: 13348

Without override the function you can directly use like this way

var kotlinConversion = { a: Int, b: Int -> print("TESTING") }

Upvotes: 3

Related Questions