java - 创建选项的通用函数
问题描述
下面的函数应该采用 a value
of 类型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()))
解决方案
是的,你必须投它: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))
推荐阅读
- spring - 如何找到 Redis 中可用的最大连接数以及使用的连接数和免费的连接数?
- php - 如果变量具有特定值,则中断
- visio - 删除 Visio 的以太网形状中的设备连接器
- scala - Spark AnalysisException:路径不存在:Azure Blob 存储
- javascript - 使用 Typescript 在组件中传递参数时出错
- node.js - 如何配置 axios 使用 SSL 证书?
- javascript - setFilesToUpload 操作对我不起作用
- python - 来自两个输入数据集的多输出回归
- javascript - 如何在变量中调用函数?
- java - Spring MVC - 接收列表的 Rest 端点可能会接收单个对象