Pattern matching on generic type in Scala

社会主义新天地 提交于 2019-11-26 11:08:25

问题


I have scala function that looks like this:

Now, depending upon the type of T (In my case, it can be Double, Boolean and LocalDate), I need to apply functions on ob. Something like this (I know the code will make no sense but I am trying to convey what I mean to do):

def X[T](ob: Observable[T]): Observable[T] = {
    //code  
    T match {
    case Double => DoSomething1(ob:Observable[Double]):Observable[Double]
    case Boolean => DoSomething2(ob:Observable[Boolean]):Observable[Boolean]
    case LocalDate => DoSomething3(ob:Observable[LocalDate]):Observable[LocalDate]
    }
}

Taking into consideration the Erasure property of Scala, can reflection be somehow used to get the job done? Is it even possible?


回答1:


I would go with TypeTag if you're on 2.10+

import reflect.runtime.universe._

class Observable[Foo]

def X[T: TypeTag](ob: Observable[T]) = ob match {
    case x if typeOf[T] <:< typeOf[Double]   => println("Double obs")
    case x if typeOf[T] <:< typeOf[Boolean]  => println("Boolean obs")
    case x if typeOf[T] <:< typeOf[Int]      => println("Int obs")
}

X(new Observable[Int])
// Int obs

See also this lengthy, but awesome answer

Note also that I only took a glimpse at scala reflection, so likely somebody may write a better example of TypeTag usage.



来源:https://stackoverflow.com/questions/21285235/pattern-matching-on-generic-type-in-scala

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!