使用默认值在Scala中进行一般类型解析

时间:2019-03-28 10:43:38

标签: scala

我正在寻找在Scala字符串与任何数字类型之间进行转换的通用功能方法。如果无法通过默认值,我需要。

例如,我需要从String转换为Int,但万一将String转换为Int失败。我需要不使用throws java.lang.NumberFormatException来传递默认值。我尝试了this的方式,但是没有得到我的想法,因为我需要它泛型,并且在出现异常的情况下也具有默认

2 个答案:

答案 0 :(得分:8)

编辑:我将解决方案更新为可以从任何类型解析为任何类型。这使解决方案根据所请求的问题变得更加通用。我认为您可以使用Scala功能方法来具有通用类型[T],但是您需要将其分为两部分。

首先实现解析类型,该解析类型可以将任何类型的[U]解析为 任何类型的[T]parseTypes使用Scala函数方式将函数canBuildFrom用作参数。然后,根据此函数的输出,您将检查它是否正确解析或是否有异常。另外,如果解析失败,您可以传递默认参数。

  def parseTypes[T,U](str: U, canBuildFrom: U ⇒ T): Either[java.lang.NumberFormatException, T] =
    Try(canBuildFrom(str)).map(Right(_)).getOrElse {
      Left(new java.lang.NumberFormatException(str.toString))
    }

  def safeParse[T,U](attributeValue: U, canBuildFrom: U ⇒ T, defaultValue: T): T = {
    parseTypes(attributeValue, canBuildFrom) match {
      case Right(x) ⇒ x
      case Left(x)  ⇒ defaultValue
      case _        ⇒ defaultValue
    }
  }


  def safeParseDoubleToBigDecimal(attributeValue: Double): BigDecimal = safeParse[BigDecimal,Double](attributeValue, toBigDecimal, 0.0)

您可以使用它来将String解析为Int,Double和Decimal,如下所示:

  def safeParseStringToInt(attributeValue: String): Int = safeParse[Int,String](attributeValue, _.toInt, 0)

  def safeParseStringToDouble(attributeValue: String): Double = safeParse[Double ,String](attributeValue, _.toDouble, 0.0)

  def safeParseStringToBigDecimal(attributeValue: String): BigDecimal = safeParse[BigDecimal ,String](attributeValue, BigDecimal(_), 0.0)
  // example of usage
  val x:Int = safeParseStringToInt("123",0)
  val y:Int = safeParseStringToInt("aaa",0)

答案 1 :(得分:3)

使用类型类可以很好地实现这种事情:

 trait ParseIt[T] {
     protected def parse(s: String): T
     def apply(s: String) = Try(parse(s)).toOption
 }

 implicit object ParseInt extends ParseIt[Int] {
   protected def parse(s: String) = s.toInt
 }

 implicit object ParseDouble extends ParseIt[Double] {
   protected def parse(s: String) = s.toDouble
 }
 // etc ...

 def parse[T : ParseIt](s: String, orElse: => T) = 
   implicitly[ParseIt[T]](s).getOrElse(orElse)

 val n: Int = parse("123", 0)
 val d: Double = parse("123", 0.0)
相关问题