Is there anyone with a deep knowledge of Kotlin ge...
# announcements
d
Is there anyone with a deep knowledge of Kotlin generics who could help me out with the API for #CCYE00YM6? I’m trying to find the least worst solution to a co/contravariance conundrum.
r
what's your problem?
d
I want to annotate nodes in a tree with transforms that will be applied to that node.
r
do you modify the node during transformation or do you create a new node? If you don't change the node, then use can use site variance =>
next: (NodeTransform<out F>
d
I don't change the node
I think I did try site variance, but maybe not there!
The annotations need to be in
interface TestAnnotation<in F>
as the transform can only apply to more general types
r
I guess you could even use
out
for NodeTransform if they don't modify
or what is the reason that made
T
invariant?
d
Is this something that you have time to screen share over - I can give you a tour?
r
nah unfortunately I don't (at least not now)
d
I'm in no rush. If you can pick a ZonedDateTime I can fit around you and make sure that I'm up to speed on what has worked and what hasn't
r
OK, I see and let you know. In the meantime, could you show me a use case where you are using the transformation
d
That's grand. You could look at https://github.com/dmcg/minutest/blob/master/core/src/test/kotlin/dev/minutest/examples/experimental/SkipAndFocusExampleTests.kt - that shows the test annotations SKIP and FOCUS. The essential issue is reconciling the contravariant TestAnnotation with the invariant NodeTransform.
r
is the type of the node of any interest during transformation?
d
I would be nice - I have places like https://github.com/dmcg/minutest/blob/master/core/src/main/kotlin/dev/minutest/experimental/flattening.kt where knowing the type allows some clever transformations
r
just asking, sometimes we make the problems harder then they need to be 😄
d
That sounds like my career
I wonder if I should start from
interface NodeTransform<F, F2: F>
r
(node as? Context<Sequence<F>, F>)
does not seem like you really use F at all. Or am I wrong?
meaning you could replace
F
with
*
and have the same functionality
d
Probably true there - but it's the Sequence<F> that corresponds to the F in the NodeTransform
r
Well, theoretically but you are using an unchecked cast which means
F
could also be something completely different
d
That's why it's private within
TestContextBuilder<Sequence<F>, F>.flatten()
- it's safe there
F isn't important in the flattening, what is important is that the Context has a parent fixture of Sequence<F> and yields individual Fs
r
but you don't know this. if you cast to
Context<Sequence<F>, F>
you only know that it is
Context
but it could also be
Context<Int, String>
and would still succeed
d
But you're asking excellent questions - definitely the person for the job
r
meaning it would fail at runtime
and thus you could also remove the type parameter all together, you don't get any additional safety like this
d
Ah yes, another logical step -
TestContextBuilder<Sequence<F>, F>
builds the Context
that the transform is applied to
So within that extension method we do know that the type of the Context is Context<Sequence<F>>, F>
r
I would try to get rid of all UNCHECKED_CAST. If you cannot, then there is no point of having a type parameter in the first place
I'll see, maybe I get some time off in the afternoon so that I can have a closer look
d
I'm trying 😉
Really appreciate it. I work from home, so ping me any time that's convenient