Stylianos Gakis
09/15/2023, 8:40 AMSomeType -> ExpectedResult. and then I need to somehow make that work with the test builder. The test builder doesn’t take in the type itself, since it uses its own DSL, so I am doing something silly like having my parameter be of the type I actually want to get from the apollo call, and then I map it to the buildBlahBlah that I need, like
FooQuery.Data {
status = when(parameterizedInput) {
1 -> buildBlah {}
2 -> buildOtherBlah {}
}
}
But it’s quite inconvenient I think, I wonder if there’s something better I can do here.Stylianos Gakis
09/15/2023, 8:53 AMoverride fun provideValues(): List<Pair<BuilderScope.() -> ContractStatusMap, IsAllowedToTerminateContract>> {
return listOf<Pair<BuilderScope.() -> ContractStatusMap, IsAllowedToTerminateContract>>(
{ it: BuilderScope -> it.buildPendingStatus {} } to IsAllowedToTerminateContract.ALLOWED,
)
}
The trick is that in the lambda I am creating, inference does not just work and I need to very explicitly say that the receiver inside the lambda is of type BuilderScope with the { it: BuilderScope -> part which I’ve never had to do before I think. This gives it access to the buildFoo() function, otherwise it was just red even if I explicitly set the type in the listOf<>() call.
Very very interesting. I think this is the way to go then, my parameterized input can be a lambda which I can use inside the builder. Just a bit tricky to get that lambda right, but it does work 😅bod
09/15/2023, 8:55 AMStylianos Gakis
09/15/2023, 8:57 AMBuilderScope.() -> ContractStatusMap but no, the lambda does not see BuilderScope in there unless I explicitly put it there with it: BuilderScope ->bod
09/15/2023, 8:58 AMbod
09/15/2023, 8:59 AMStylianos Gakis
09/15/2023, 10:34 AMbod
09/15/2023, 10:40 AMBuilderScope -> ContractStatusMap instead of BuilderScope.() -> ContractStatusMap ?Stylianos Gakis
09/15/2023, 10:42 AMBuilderScope.() -> ContractStatusMap or (BuilderScope) -> ContractStatusMap right?
In either case, the compiler can use either one of them interchangeably in this context as far as I can tell.
It’s just that in the lambda itself I can’t somehow denote that it’s BuilderScope.() -> ContractStatusMap so I pass it as it which would mean that it’s like (BuilderScope) -> ContractStatusMap yeah.
But yeah, it knows how to figure out “`T.() -> Unit` <-> `(T) -> Unit`” by itself.Stylianos Gakis
09/15/2023, 10:42 AMStylianos Gakis
09/15/2023, 10:46 AMbod
09/15/2023, 12:34 PM(BuilderScope) -> ContractStatusMap
But still something I don't get, e.g.
val lambda1: (String) -> Int = { it: String -> it.toInt() }
val lambda2: String.() -> Int = { it: String -> it.toInt() }
lambda2 doesn't compile for mebod
09/15/2023, 12:36 PMval listOfLambda1 = listOf<(String) -> Int>({ it: String -> it.toInt() })
val listOfLambda2 = listOf<String.() -> Int>({ it: String -> it.toInt() })
does compile.
🪄 indeedStylianos Gakis
09/15/2023, 12:37 PMit: String -> there you do not get the String as a receiver in the lambda right?bod
09/15/2023, 12:37 PMval listOfLambda2 = listOf<String.() -> Int>({ this.toInt() })
also compilesbod
09/15/2023, 12:38 PMbod
09/15/2023, 12:39 PMStylianos Gakis
09/15/2023, 12:40 PM