Randomize Randomize - 1 year ago 70
Scala Question

Scala: How to avoid check instance and casting

I found myself sometimes in the condition of running:


just to check if the collection has the instances of
and casting the final filtered collection.

Is there a better way to do that (assuming I cannot change the current data structure)?

I tried with:

someCollection.map(case c: Foo => c)

but at runtime it returns
as of course it is looking for all the remaining cases (
case _ =>

Answer Source
someCollection.collect { case c: Foo => c }
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download