将String解析为Boolean或Int

时间:2015-11-03 14:29:25

标签: scala

要将字符串解析为int,我们可以theString.toInt和布尔theString.toBoolean。我怎样才能使这个通用?

我想以某种方式参数化一个函数,以便我可以尝试将字符串解析为boolean或int,处理错误并返回错误时的默认值等。

我有这个:

  def tryParsing[T: TypeTag](value: String)(implicit errorAccumulator: Accumulator[Int]): Option[T] = {

    // scala's .toBoolean only permits exactly "true" or "false", not numeric booleans.
    val validBooleans = Map(
      "true" -> true,
      "false" -> false,
      "1" -> true,
      "0" -> false
    )

    import scala.reflect.runtime.universe._

    // doesn't work. Also, using TypeTag doesn't seem to work.
    typeOf[T] match {
      case t if t <:< typeOf[Boolean] =>
        val result = validBooleans.get(value.asInstanceOf[String].toLowerCase)

        if (result.isEmpty) {
          logger.warn(s"An error occurred parsing the boolean value `$value`")
          errorAccumulator += 1
        }

        result.asInstanceOf[Option[T]]

      case _ =>
        Try(value.asInstanceOf[T]) match {

          case Success(x) => Some(x: T)
          case Failure(e) =>
            logger.warn(s"An parsing error occurred: $e")
            errorAccumulator += 1
            None
        }
    }
  }

我无法匹配类型标签。我想这是因为如果value的类型为T,那么typetag会阻止其类型被删除。但在这里,我想称之为:

tryParsing[Boolean]("1")         // value from variable

如何匹配类型或以其他方式执行我在scala 2.10中尝试做的事情?

1 个答案:

答案 0 :(得分:7)

使用类型类模式:

trait Parser[T] {
  def parse(input: String): Option[T]
}

def parse[T](input: String)(implicit parser: Parser[T]): Option[T] =
  parser.parse(input)

import util.Try
implicit object IntParser extends Parser[Int] {
  def parse(input: String) = Try(input.toInt).toOption
}
implicit object BooleanParser extends Parser[Boolean] {
  def parse(input: String) = Try(input.toBoolean).toOption
}

瞧:

scala> parse[Int]("3")
res0: Option[Int] = Some(3)

scala> parse[Int]("zzz")
res1: Option[Int] = None

scala> parse[Boolean]("true")
res2: Option[Boolean] = Some(true)

scala> parse[Boolean]("zzz")
res3: Option[Boolean] = None