jbrown
jbrown

Reputation: 7986

Parsing a String to Boolean or Int

To parse a string to an int we can do theString.toInt, and to a boolean theString.toBoolean. How can I make this generic?

I want to somehow parameterise a function so that I can try to parse the string as either a boolean or int, handle errors and return defaults on error, etc.

I've got this:

  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
        }
    }
  }

I can't match on the type tag. I guess this is because if value was of type T then the typetag would prevent its type from being erased. But here, I want to call this like:

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

How can I match on types or otherwise do what I'm trying to do in scala 2.10?

Upvotes: 1

Views: 5225

Answers (1)

Seth Tisue
Seth Tisue

Reputation: 30453

Use the typeclass pattern:

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
}

Voila:

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

Upvotes: 7

Related Questions