https://kotlinlang.org logo
#codereview
Title
# codereview
c

christophsturm

08/06/2022, 7:37 PM
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

ephemient

08/06/2022, 8:26 PM
how would you expect a
doit<NotUnit>()
call to work?
c

christophsturm

08/06/2022, 8:51 PM
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

ephemient

08/06/2022, 8:52 PM
which lambda are you trying to default?
c

christophsturm

08/06/2022, 8:52 PM
the given lambda
the usual case is no given is given, and so its unit ContextDependency is Unit
e

ephemient

08/06/2022, 8:53 PM
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

christophsturm

08/06/2022, 8:57 PM
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

ephemient

08/06/2022, 9:04 PM
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

christophsturm

08/06/2022, 9:06 PM
ok so the default parameter can not force the generic type into a type?
e

ephemient

08/06/2022, 9:06 PM
no
c

christophsturm

08/06/2022, 9:07 PM
do you mean its impossible because the kotlin compiler does not support it, or because it conceptually wrong?
e

ephemient

08/06/2022, 9:08 PM
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

Giorgos Makris

08/06/2022, 9:11 PM
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

ephemient

08/06/2022, 9:13 PM
I consider that isomorphic to the "just write an additional overload" solution since you're writing two implementations (one forwarding to the other)
c

christophsturm

08/07/2022, 11:04 AM
if by isomorphic you mean that one solution is super simple and the other one very complex, then i agree 🙂
e

ephemient

08/07/2022, 1:50 PM
by isomorphic I mean that they have the same shape
c

christophsturm

08/07/2022, 1:57 PM
ok but isnt that just a fancy way of saying equivalent?
e

ephemient

08/07/2022, 2:35 PM
it literally means "same shape". doesn't have to be equivalent in complexity to be isomorphic
c

christophsturm

08/08/2022, 8:30 AM
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

ephemient

08/09/2022, 12:55 AM
it helps in saying that the two solutions do not differ in this regard, and your decision should be guided by other concerns
4 Views