Note that all observables contained in the result of groupedObs
are Observable[MyCaseClass]
and not Observable[CaseOne]
or Observable[CaseTwo]
. So if there was no erasure and the type check did work you would get MatchError
instead. In fact, in your case observables under each key will include both CaseOne
and CaseTwo
elements.
So yes, you need to include the extra data. You have two options.
1) Make the classes part of the key:
val groupedObs : Observable[((Boolean, Class[_]), Observable[MyCaseClass])] =
Observable(1 to 20) map {
x => if (x > 10) CaseOne(x) else CaseTwo(x)
} groupBy {
case CaseOne(x) => (x % 2 == 0, classOf[CaseOne])
case CaseTwo(x) => (x % 2 == 0, classOf[CaseTwo])
}
groupedObs subscribe ( (onNext : ((Boolean, Class[_]), Observable[MyCaseClass])) => onNext match {
case ((even : Boolean, c: Class[_]), o : Observable[_]) if c == classOf[CaseOne] => println("Case one")
case ((even : Boolean, c: Class[_]), o : Observable[_]) if c == classOf[CaseTwo] => println("Case two")
}
)
2) Check the elements of the nested observable:
// groupedObs as in your code, not as above
groupedObs subscribe {
case (even : Boolean, o : Observable[MyCaseClass] ) =>
o subscribe {
case _: CaseOne => println("Case one")
case _: CaseTwo => println("Case two")
}
}
This prints output for every element of the original observable; alternately, you can stop after the first element (e.g. because you know all elements for the same key have the same class), etc.