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.
trait ConverterFor[T] {
def convert(s: String): Option[T]
}
object ConverterFor {
implicit def forInt = new ConverterFor[Int] {
def convert(s: String) = Try(s.toInt).toOption }
implicit def forDouble = ...
}
def convert[T](s: String)(implicit converter: ConverterFor[T]): Option[T] =
converter.convert(s)
The correct ConvertorFor
is resolved implicitly at compile time. If you try to call convert
with a type for which there is no implicit ConverterFor
available, it will fail to compile.