You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently T in Assertion.Builder is invariant. Is there a specific reason for that?
I have stumbled upon a problem with a custom assertion I was writing. I have a structure which is essentially a tree of maps:
data classNode(id:String, children:MutableMap<String, Node>)
funbuildTree(): Map<String, Node> =TODO("irrelevant")
I wanted to create two assertions:
fun Assertion.Builder<Map<String, Node>>.node(
id:String,
childrenAssertion:Assertion.Builder<Map<String, Node>>.() ->Unit
) {
withValue(id) {
get { this.id }.isEqualTo(id)
get { children }.childrenAssertion()
}
}
fun Assertion.Builder<Map<String, Node>>.leaf(id:String) {
withValue(id) {
get { this.id }.isEqualTo(id)
get { children }.isEmpty()
}
}
// usage
expectThat(buildTree()) {
node("node1") {
node("node2") {
leaf("leaf1")
}
leaf("leaf2")
leaf("leaf3")
}
}
But it didn't work because childrenAssertion binds T to Map<*, *> where get { children } returns builder with T bound to MutableMap<*, *>. The solution was to use call-site projection:
fun Assertion.Builder<outMap<String, Node>>.node(
id:String,
childrenAssertion:Assertion.Builder<outMap<String, Node>>.() ->Unit
)
But then I wondered why Assertion.Builder is not covariant in the first place? What would be the drawbacks of defining it as Assertion.Builder<out T>?
The text was updated successfully, but these errors were encountered:
Currently
T
inAssertion.Builder
is invariant. Is there a specific reason for that?I have stumbled upon a problem with a custom assertion I was writing. I have a structure which is essentially a tree of maps:
I wanted to create two assertions:
But it didn't work because
childrenAssertion
bindsT
toMap<*, *>
whereget { children }
returns builder withT
bound toMutableMap<*, *>
. The solution was to use call-site projection:But then I wondered why
Assertion.Builder
is not covariant in the first place? What would be the drawbacks of defining it asAssertion.Builder<out T>
?The text was updated successfully, but these errors were encountered: