David Kubecka
04/04/2023, 1:09 PMfun <T> foo(arg: String, handler: (String) -> T = {}): T
I would like to define the function so that it's possible to call it with a default parameter:
foo("abc")
David Kubecka
04/04/2023, 1:10 PMfoo<Unit>("abc")
would be fine.David Kubecka
04/04/2023, 1:11 PM[TYPE_MISMATCH] Type mismatch.
Required:
T
Found:
Unit
David Kubecka
04/04/2023, 1:12 PMJoffrey
04/04/2023, 1:12 PMT
. If you want to provide a default value for that one, it has to match any possible T
that someone would use on the call site, which is generally not possible either, unless your default lambda just throws and returns Nothing
🙂Joffrey
04/04/2023, 1:12 PMfun foo(arg: String): Unit = foo<Unit>(arg) {}
fun <T> foo(arg: String, handler: (String) -> T): T {
...
}
Joffrey
04/04/2023, 1:13 PMOf course, the real-life function is more complex so e.g. simply using overloading isn't a good optionThen please show the real-life function 😉
David Kubecka
04/04/2023, 1:15 PMDavid Kubecka
04/04/2023, 1:18 PMfun foo(arg: String): Unit = foo(arg) {}
and not being able to convert this to a default parameter 🙂Joffrey
04/04/2023, 1:19 PMDavid Kubecka
04/04/2023, 1:19 PMTobias Suchalla
04/05/2023, 5:30 AMfun <T> foo(arg: String, handler: ((String) -> T)? = null): T
But I guess that won't help you to return T from that function...