How about just:
def mapObjects[T](lst: GenSeq[HasGetter[T]]): GenSeq[T] = {
lst map {v => v.getT}
}
scala> mapObjects(objs)
res18: scala.collection.GenSeq[String] = List(abc, def, hij)
There is no real advantage of using S <: HasGetter[T]
in your case as you are just doing a map over lst
and obtaining elements of type T
. And your return type of the function has got nothing to with type S
. Hence there is no need to get the precise type S
Had the return type of the function or function body explicitly needed to maintain the type S <: HasGetter[T]
, your code would be needed. But in this case there is no point maintaining the type.
Moreover GenSeq[+A]
is a covariant type. So by default it can accept types belonging to GenSeq[ <: HasGetter[_]]
in the argument. Has GenSeq
not been co-variant, it would have been a different Issue.