I'm a bit baffled by the Koltin lambdas and I wanted to know how to use it given the following snippet of code:
interface KotlinInterface {
fun doStuff(str: String): String
}
And the function that requires this Interface to be passed as a parameter:
fun kotlinInterfaceAsArgument(kotlinInterface: KotlinInterface): String{
return kotlinInterface.doStuff("This was passed to Kotlin Interface method")
}
fun main(args: Array<String>){
val newString = kotlinInterfaceAsArgument({
str -> str + " | It's here" //error here (type mismatch)
})
}
However, the same logic but in Java compiles and runs as intended.
public class JavaClass {
public String javaInterfaceAsArgument(JavaInterface javaInterface){
String passedToInterfaceMethod = "This was passed to Java Interface method";
return javaInterface.doStuff(passedToInterfaceMethod);
}
public interface JavaInterface {
public String doStuff(String str);
}
}
and
public class Main {
public static void main(String[] args) {
JavaClass javaClass = new JavaClass();
String newValue = javaClass.javaInterfaceAsArgument(str -> str + " | It's here!");
System.out.println(newValue);
}
}
How can I utilize lambda in Kotlin in this case?