mardi 31 juillet 2018

How to make scala List of Any have a mandatory attribute?

I'm new to scala and OOP in general, therefore I apologize in advance if the question is stupid, but I really don't know how to find what I'm looking for.

I have the following code

case clase MyClass(id: Int, active: Boolean)
case class MyOtherClass(name: String, active: Boolean)


private def myMethod(active: Boolean, list: List[MyClass]): List[MyClass] = {
  if(active){
      list.filter(_.active == true)
  }else{
      list.filter(_.active == false)
  }
}

Ok now, what I want to do is to make this "myMethod" take a List of "Any" instead of a specific class as long as it has an "active" attribute. How should I go about it?

For instance, the class:

 case class MyThridClass(name: String)

wouldn't work since it doesn't have an "active" attribute

Aucun commentaire:

Enregistrer un commentaire