Is there a way to cast a lambda say () -> Unit
to a (String) -> Unit
? Specifically in constructors?
Currently, my get around is just wrapping it with another lambda as follows
val x: () -> Unit = { println("x has fired") }
val xMapped: (String) -> Unit = { x() }
Why does the compiler not neglect the argument in this case so I can do
val xMappedBetter: (String) -> Unit = x // Can't cast!
In fact, if it was really smart, and you didn’t have duplicated parameters couldn’t it even handle
(String, Int, Double, Float) -> Unit
to (Float) -> Unit
or (Int, Float) -> Unit
?
Perhaps I am naive, or perhaps a solution already exists. But overloading a constructor 5 times to wrap a lambda seems unnecessary.