假设我需要编写一个函数convert[T]: String => Option[T]
如下,它的工作原理:
import scala.util.Try
def toInt(s: String): Option[Int] = Try(s.toInt).toOption
def toDouble(s: String): Option[Double] = Try(s.toDouble).toOption
def toBoolean(s: String): Option[Boolean] = Try(s.toBoolean).toOption
// if T is either Int, Double, or Boolean return
// toInt(s), toDouble(s), toBoolean(s) respectively
def convert[T](s: String): Option[T] = ???
我应该使用TypeTag
来实现呢?