具有特征泛型类型的scala重载方法

时间:2016-06-02 04:03:27

标签: scala generics traits

当一个类在scala中重载泛型类型特征时,我遇到了问题: 代码是这样的:

class A extends B[A]{
   def -(string:String):A={
       println("string")
       ...
   }
}
trait B[T] {
   def -(t:T):T{
       println("a")
       ...
   }
}
带有a1和a2的

是A类的两个实例,我调用a1-a2。我期待的是"打印"但我得到"打印字符串"代替。 它似乎与通用类型的特性有关,因为当我从B中删除它并强制类型为A时:

trait B {
       def -(t:A):A{
           println("a")
           ...
       }
    }

它正常工作。

为什么会发生这种情况?有什么解决方案可以解决这个问题吗?

正如评论所要求的,这里是我和一些现实世界的代码。

trait MapComputable[T]{

  val plus =(a:Double ,b:Double) => a+b
  val minus =(a:Double ,b:Double) => a-b
  val times =(a:Double ,b:Double) => a*b
  val quot =(a:Double ,b:Double) => a/b

  def + (map: T): T =this.op(map,plus);
  def - (map: T): T =this.op(map,minus);
  def * (map: T): T = this.op(map,times)
  def / (map: T): T = this.op(map,quot)

  def op (map: T,op :(Double,Double)=>Double): T
}

trait MapComputableResultMap{

  val plus =(a:Double ,b:Double) => a+b
  val minus =(a:Double ,b:Double) => a-b
  val times =(a:Double ,b:Double) => a*b
  val quot =(a:Double ,b:Double) => a/b

  def + (map: ResultMap): ResultMap =this.op(map,plus);
  def - (map: ResultMap): ResultMap =this.op(map,minus);
  def * (map: ResultMap): ResultMap = this.op(map,times)
  def / (map: ResultMap): ResultMap = this.op(map,quot)

  def op (map: ResultMap,op :(Double,Double)=>Double): ResultMap
}

class ResultMap(val impl:Map[String,Double]) extends Map[String,Double] with collection.immutable.MapLike[String, Double, ResultMap]
with MapComputable[ResultMap] {

  override val empty : ResultMap = ResultMap.empty

  override def +[B1 >: Double](kv: (String, B1)): Map[String, B1] = impl + kv

  def +(kv:(String,Double)):ResultMap=new ResultMap(impl +kv)

  def ++(that : ResultMap):ResultMap=new ResultMap(impl++that)

  override def get(key: String): Option[Double] = impl.get(key)

  override def iterator: Iterator[(String, Double)] = impl.iterator

  override def -(key: String): ResultMap = new ResultMap(impl-key)

  def op (resultMap: ResultMap,op :(Double,Double)=>Double): ResultMap ={
    val set=this.keySet & resultMap.keySet
    val kv=for(k <- set.toList;value=op(this(k) , resultMap(k))) yield (k,value)
    ResultMap(kv: _*)
  }

  def op (double:Double,op : (Double,Double)=>Double): ResultMap ={
    val kv=for(k <- this.keySet.toList;value=op(this(k),double)) yield (k,value)
    ResultMap(kv: _*)
  }

}
object ResultMap{
  val empty: ResultMap=new ResultMap(Map.empty)

  def apply(elems: (String, Double)*) =new ResultMap(elems.toMap)

  def main(args: Array[String]): Unit ={
    var resultMap=ResultMap("a"->3.0,"b"->2.0)
    resultMap-=ResultMap("a"->10,"b"->1.0,"c"->12)
    println(resultMap)
  }
}

当我使用MapComputableResultMap扩展时,它工作正常,但在使用Computable [ResultMap]时,它面临以下错误

Exception in thread "main" java.lang.ClassCastException: com.vip.vrc.test.ResultMap cannot be cast to java.lang.String
        at com.vip.vrc.test.ResultMap.$minus(ResultMap.scala:33)
        at com.vip.vrc.test.ResultMap$.main(ResultMap.scala:69)
        at com.vip.vrc.test.ResultMap.main(ResultMap.scala)

在这种情况下,只有减号( - )可以解决这个问题。

0 个答案:

没有答案