Suppose I need to write a function convert[T]: String => Option[T]
, which works as follows:
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] = ???
Should I use TypeTag
to implement it ?
No, you should use the typeclass pattern. That way the types are resolved at compile time rather than runtime, which is much safer.
The correct
ConvertorFor
is resolved implicitly at compile time. If you try to callconvert
with a type for which there is no implicitConverterFor
available, it will fail to compile.