https://kotlinlang.org logo
Title
r

Ruckus

12/21/2018, 10:47 PM
This seems like a pretty basic question, but it's just not clicking for some reason. Say I have
class Ref<T: Ref<T>> { ... }
class RefHolder<T: Ref<T>> { ... }

class Source {
    val refs: MutableList<RefHolder<*>> = ...
    ...
}

fun <T: Ref<T>> serializeRef(ref: RefHolder<T>, ...): ... { ... }

fun serializeSource(source: Source): ... {
  val refs = source.refs.map { serializeRef(it, ...) }  // Cannot resolve type as it: Ref<*>
}
How do I make it so I can call
serializeRef
?
d

Dominaezzz

12/21/2018, 11:09 PM
You could make
serializeRef
to be
<T : Ref<*>>
and
ref: T
or make
Source
generic too?
r

Ruckus

12/22/2018, 3:03 PM
Neither of those will work sadly. The source ref list must be able to hold any type of ref, and the
serializeRef
needs the exact type for further calls.
d

Dominaezzz

12/22/2018, 3:07 PM
Ah, change
serializeRef
to take
ref: T
instead
ref: Ref<T>
. Not sure if it'll fix it but it'll at least shorten your code. 😅
You probably need covariance or contravariance somewhere in your generics somewhere.
r

Ruckus

12/22/2018, 4:10 PM
Oops, I wrote the code wrong. I've updated it to fix my mistake, so now you can see why
ref: T
won't work.