mardi 5 février 2019

How to create Map of Class type in scala

There is one use case, where i am trying to create a Map of String and Case Class type.

For example:

  case class CaseClass1(foo: String, bar: String)
  val myCaseClasses = Map("1" -> CaseClass1, "2" -> CaseClass2, "3" -> CaseClass3)

And using myCaseClasses map, i am want to iterate through all the classtypes and pass that type to another function (Just trying to replicate my use case, its not working code).

  def nullFillForCaseClasses(init: String]) = {
    myCaseClasses.foldLeft(ds) {
      case (str, (classNo, classType)) =>
        str + getNullClassOf[lobType.type](classNo)
    }
  }

  def getNullClassOf[T <: Product : TypeTag](classNo: String): String = 
                                                         someFunction[T](classNo)

I am not sure this is possible by scala or not. This can be done by simple if else or pattern matching, but playing with scala and figuring other ways also.





Aucun commentaire:

Enregistrer un commentaire