samedi 23 avril 2016

Scala: Set a value based on parameter-type

I have a beginner question related to scala implicit:

class Box[T] {
  var value = Option(None)

  def pickValueUsingTypeOfT [T]: Unit = {
    val list = List("string", 1, new myClass(1D) )
    //value = Option ( find right element from the 'list' )
  }
}

class myClass(double: Double) {}


object Setter {

  def main(args: Array[String]) {
    val n: Box[String] = new Box[String]
    n.pickValueUsingTypeOfT
  }
}

The objective is to set the value according to type of T.





Aucun commentaire:

Enregistrer un commentaire