Hello,
I’m having issues with an implementation of the following:
There is a case class A that stores some value of a generic type and associated meta-information, and also there is a collection that stores this case classes. What I want to achieve is to restrict the type of the collection element to be exactly of type A with any generic argument. The example below:
case class A[B](..., val value: B) {
def equal(that: A[B]) = ???
}
class Coll[T[P] <: A[P]] private (val collection: Array[T] = Array.empty[T]) {
def update(a: A): Option[A] = {
...
if (...) {
Some(a.copy(...)) // a.value is untouched
} else {
None
}
// Issue here as return type of the a.copy() is A[_]
}
def add(a: A) = {
val updated = update(a)
...
}
}
However, when I compile the code there are some issues, like method update returns a value of type A[_] instead of type T, type T in the class variables initialization takes type parameter, method equal expects argument of type A[_], etc. How can I achieve expected behavior and make class Coll parameterized only on instance of a class A?
Many thanks!