is there a way to get this to work? ```fun <Res...
# codereview
c
is there a way to get this to work?
Copy code
fun <Result> doit(lambda: () -> Result={Unit}) {

}
when the lambda is not passed i want Result to be of type Unit. I get the error Required: Result, found: Unit.
currently i do it without default parameter and have a seconf function that has no lambda parameter and calls the first function with
{}
e
how would you expect a
doit<NotUnit>()
call to work?
c
this is just a reduced example. maybe its probably too reduced šŸ™‚.
Copy code
suspend fun <ContextDependency> context(
        name: String,
        given: (suspend () -> ContextDependency),
        contextLambda: suspend ContextDSL<ContextDependency>.() -> Unit
    )
this is how it originally looks
e
which lambda are you trying to default?
c
the given lambda
the usual case is no given is given, and so its unit ContextDependency is Unit
e
yeah I can't imagine that working then. there's always going to be a way of writing
context("name") { /* inferred context of ContextDSL<something other than Unit> */ }
c
ok but shouldnā€™t the default paramter be basically the same then the overloaded function that Iā€™m now using?
Copy code
override suspend fun context(name: String, function: ContextLambda) {
            context(name, {}, function)
        }
e
no, because it's specialized
when you write a
fun <T> foo()
, the caller can choose any
T
(up to constraints you declare on it). you can't constrain it differently based on whether a parameter is present or defaulted
c
ok so the default parameter can not force the generic type into a type?
e
no
c
do you mean its impossible because the kotlin compiler does not support it, or because it conceptually wrong?
e
https://youtrack.jetbrains.com/issue/KT-52519/Allow-the-default-argument-in-a-function-to-dictate-the-type-if-no-argument-is-supplied currently the compiler does not support it, but I also don't see a good plan for how to actually change the language to enable it
g
There is a way to do it. You would basically create a sealed interface which will be implemented by different data classes, each data class will have 1 field, the function you want to use. Basically functions as algebraic data type šŸ˜›. The downside is that you will have to pattern-match the parameter to the correct "function"
Your default will also be explicit
e
I consider that isomorphic to the "just write an additional overload" solution since you're writing two implementations (one forwarding to the other)
c
if by isomorphic you mean that one solution is super simple and the other one very complex, then i agree šŸ™‚
e
by isomorphic I mean that they have the same shape
c
ok but isnt that just a fancy way of saying equivalent?
e
it literally means "same shape". doesn't have to be equivalent in complexity to be isomorphic
c
Iā€™m just trying to understand when ā€œsolution a is isomorphic to solution bā€ is a helpful statement, and what decisions it would help someone make.
e
it helps in saying that the two solutions do not differ in this regard, and your decision should be guided by other concerns