首页 > 解决方案 > 创建选项的通用函数

问题描述

下面的函数应该采用 a valueof 类型String并返回Option与函数的第二个参数的类型匹配的 an,例如

toOption[Double]("10", classOf[Double])

def toOption[A](value: String, classType: A): Option[A] = {
    classType match {
        case _: Int => {
            try {
                Some(value.trim().toInt)
            } catch {
                case e: NumberFormatException => None
            }
        }
        case _: Double => {
            try {
                Some(value.trim().toDouble)
            } catch {
                case e: NumberFormatException => None
            }
        }
        case _: java.sql.Timestamp => {
            try {
                Some(java.sql.Timestamp.valueOf(value.trim()))
            } catch {
                case e: NumberFormatException => None
            }
        }
        case _ => None
    }
}

但是,由于目前的功能,我收到以下错误。我如何/应该如何解决这些错误?

<console>:15: error: type mismatch;
 found   : Int
 required: A
                       Some(value.trim().toInt)
                                         ^
<console>:22: error: type mismatch;
 found   : Double
 required: A
                       Some(value.trim().toDouble)
                                         ^
<console>:29: error: type mismatch;
 found   : java.sql.Timestamp
 required: A
                       Some(java.sql.Timestamp.valueOf(value.trim()))

标签: javascalagenericsjvm

解决方案


是的,你必须投它:Some(value.trim.toInt).asInstanceOf[A]。它不知道 A 为Int

这是一个更好的方法:

 trait FromString[T] { 
   def convert(s: String): T
   def apply(s: String): Option[T] = Try(convert(s.trim)).toOption
 }

 implicit object IntFromString extends FromString[Int] {
   def convert(s: String) = s.toInt
 }
 implicit object DoubleFromString extends FromString[Double] {
   def convert(s: String) = s.toDouble
 } 
 // etc.

所以,现在,你可以写:

def toOption[T : FromString](s: String): Option[T] = implicitly[FromString[T]](s)

或者,如果您想在None未定义转换时获得回报:

def toOption[T](s: String)(implicit conv: FromString[T] = null) = Option(conv)
 .flatMap(_.apply(s))

推荐阅读