This code prints "Hello 1". parseInput have two generic types, the first arg is a simple object of the first generic type A, the second arg is a function which suppose to change the generic type A to generic B. As you can see it works fine in the following.
fun toGreeting(input : Int) : String {
return "hello " + input
}
fun <A, B> parseInput(raw : A, toB : (raw : A) -> B) : B {
return toB(raw)
}
fun main(args: Array<String>) {
val greeting = parseInput(1, ::toGreeting)
println(greeting)
}
The question is how can I give a default lambda value for the second named argument in the parseInput. So I can call this function by just providing the first argument, and having it to use the default lambda function.
val greeting = parseInput(1)
println(greeting)
(Any) -> Anyfunction as the default here. Since there's generics involved, a generic(A) -> Bfunction is required, which then forces the caller to specify the generic types, as there's no way to inferB. As for the implementation of this default function, you can either cast something toBinside it (which probably won't succeed), or have it returnNothing(which is not what you want here).greetingin your second example to be equal toUnit? How is that useful?