title: Best practice to filter case class object attributes
body: <p>Say I have case classes like </p>
  
  <pre><code>case class Person(name:String, id: Int)
  case class Country(name:String, population: Long, continent:String)
  case class City(name: String, place: Country)
  </code></pre>
  
  <p>Now I would like to make a search operation using this piece of generic code</p>
  
  <pre><code>def search[T](searchIn: Seq[T], seachFor:String) : Seq[T] = {
       val result = searchIn flatMap {
         case p: Person if(p.name.contains(searchFor)) =&gt; Some(p)
         case c: Country if(c.continent.contains(searchFor)) =&gt; Some(c)
         case c: City if(c.name.contains(serachFor)) =&gt; Some(c)
         case _ =&gt; None 
     }
    result.map(_.asInstanceOf[T])
  }
  </code></pre>
  
  <p>Challenge here is I might need to make search in few more attributes for existing case classes or I need to have more cases classes.</p>
  
  <p>I don't feel like the above code is generic as I need to know which object it is, search in it and convert it back.</p>
  
  <p>What is best approach to this type of problem?</p>