【问题标题】:Scala implicit Any wrapperScala 隐式任何包装器
【发布时间】:2012-11-21 10:12:26
【问题描述】:

我正在尝试为我正在编写的一个简单数据库的值编写一个包装器。目前,我有一个 Value 类,它的类型是ordered 的子类型(这很重要,因为我将对这些值进行比较)。我有 Int、Double、String、Boolean 和 Date 类型的隐式包装器。我的问题是,当我尝试将 Any 类型的内容包装在 Value 中时,我收到一个错误。

这是我的价值类:

case class Value[A <% Ordered[A]](value: A) extends Ordered[Value[A]] {

  def matches(otherType: A): Boolean = {
    if(value.getClass == otherType.getClass)
      true
    else false
  }

  override def toString() = {
    value.toString
  }

  def compare(other: Value[A]) = value compare other.value
}

object Value {
  implicit def intVal(some: Int) = new Value[Int](some) {
    def compare(other: Int): Int = value compare other
  }
  implicit def doubleVal(some: Double) = new Value[Double](some) {
    def compare(other: Double): Int = value compare other
  }
  implicit def stringVal(some: String) = new Value[String](some) {
    def compare(other: String): Int = value compare other
  }
  implicit def boolVal(some: Boolean) = new Value[Boolean](some) {
    def compare(other: Boolean): Int = value compare other
  }
  implicit def dateVal(some: Date) = new Value[Date](some) {
    def compare(other: Date): Int = value.compareTo(other)

    override def toString() = {
      var formatter = new SimpleDateFormat("dd/MM/yyyy")
      formatter.format(value)
    }
  }
//  implicit def anyVal(some: Any) = new Value[Any](some){
//    def compare(other: Any) = {
//      
//    }
//  }

  var x = new Value(0)
}

在另一个类中,我有一个 List[Any] 我想将其包装为一个值(我知道 Any 是正确的类型之一)。不幸的是,我无法将 Any 包装在一个值中,因为它不是 Ordred 类型。

另外,如果我可以使用泛型 Value 作为类型参数,我的问题将得到解决,但我遇到了问题:

我知道将值放入参数中会更有意义,但是当我将值解析到程序中时,我无法解析类型

例如我想做的:

def integer: Parser[Value] = """\d+""".r ^^ {x => Value(augmentString(x).toInt)}

但是,它抱怨 Parser[Value] 需要一个类型。 这不是问题,因为我可以创建类型 [Value[Int]] 但是,当我有这样的组合器方法时:

def value: Parser[Value[Something]] =  integer ||| real | date 

def getSome(some: Value[Something])

我没有包含所有五种可能类型的通用值。

对我正在尝试的事情有更好的解决方案吗?

【问题讨论】:

    标签: scala types implicit-conversion implicit


    【解决方案1】:

    要让def value: Parser[Value[Something]] = integer ||| real | date 工作,你真的需要类似的东西..

    trait Value
    case class StringValue(s: String) extends Value
    case class IntValue(i: Int) extends Value
    case class DateValue(d: Date) extends Value
    

    【讨论】:

      【解决方案2】:

      也许您可以使用一种在 scala 中编码不相交联合类型的方法(请参阅:Does Scala have “type disjunction” (union types)?

      例如,如果您使用 "stuttering or" 方法,您可以定义:

      case class Value[A <% Ordered[A] <% String or Int or Double or Boolean](value: A) extends Ordered[Value[A]] {
      
        def matches(otherType: A): Boolean = {
          if(value.getClass == otherType.getClass)
            true
          else false
        }
      
        override def toString() = {
          value.toString
        }
      
        def compare(other: Value[A]) = value compare other.value
      }
      

      然后你可以:

      def integer[T <% Value[T]] = """\d+""".r ^^ {x => Value(augmentString(x).toInt)}
      

      【讨论】:

        猜你喜欢
        • 2011-07-23
        • 2014-06-13
        • 2017-09-15
        • 2020-12-08
        • 2013-02-23
        • 2011-02-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多