Scala中的Beginner for循环:如何声明泛型元素?

时间:2013-10-08 23:02:49

标签: scala generics

我是Scala的新手,我遇到了一个简单的通用for循环声明的问题,其中我的类的一个实例FinSet [T]与我的另一个FinSet [T]实例“联合”,其他。这是我目前的U(Union简称)的实现:

def U(other:FinSet[T]) = {
  var otherList = other.toList
  for(otherElem <- 0 until otherList.length){
    this.+(otherElem)
  }
  this
}

尝试编译时,会收到此错误。

error: type mismatch:
found:   : otherElem.type (with underlying type Int)
required : T
    this.+(otherElem)

这是在类ListSet [T]中,它是抽象类FinSet [T]的扩展。两者都显示在这里:

abstract class FinSet[T] protected () {
 /* returns a list consisting of the set's elements */
  def toList:List[T]

  /* given a value x, it retuns a new set consisting of x
     and all the elemens of this (set)
  */
  def +(x:T):FinSet[T]

  /* given a set other, it returns the union of this and other,
     i.e., a new set consisting of all the elements of this and
     all the elements of other
  */
      def U(other:FinSet[T]):FinSet[T]  

  /* given a set other, it returns the intersection of this and other,
     i.e., a new set consisting of all the elements that occur both
     in this and in other
  */
  def ^(other:FinSet[T]):FinSet[T]

  /* given a set other, it returns the difference of this and other,
     i.e., a new set consisting of all the elements of this that
     do not occur in other
  */
  def \(other:FinSet[T]):FinSet[T]

  /* given a value x, it retuns true if and only if x is an element of this 
  */
  def contains(x: T):Boolean

  /* given a set other, it returns true if and only if this is included
     in other, i.e., iff every element of this is an element of other
  */
  def <=(other:FinSet[T]):Boolean = 
    false // replace this line with your implementation


  override def toString = "{" ++ (toList mkString ", ") ++ "}"

  // overrides the default definition of == (an alias of equals)
  override def equals(other:Any):Boolean = other match {
    // if other is an instance of FinSet[T] then ...
    case o:FinSet[T] => 
      // it is equal to this iff it includes and is included in this
      (this <= o) && (o <= this)
    case _ => false
  }
}

在这里,ListSet:

class ListSet[T] private (l: List[T]) extends FinSet[T] {
  def this() = this(Nil)

  // invariant: elems is a list with no repetitions
  //            storing all of the set's elements 
  private val elems = l

  private def add(x:T, l:List[T]):List[T] = l match {
    case Nil => x :: Nil
    case y :: t => if (x == y) l else y :: add(x, t)
  }

  val toList = 
    elems

  def +(x: T) = 
    this.toList.+(x)

  def U(other:FinSet[T]) = {
    var otherList = other.toList
    for(otherElem <- 0 until otherList.length){
      this.+(otherElem)
    }
    this
  }

  def ^(other:FinSet[T]) = 
    this

  def \(other:FinSet[T]) = 
    this

  def contains(x:T) = 
    false
}

我错过了一些明显的东西吗?

1 个答案:

答案 0 :(得分:1)

在for循环中,您将Int分配给otherElemx until y生成Range[Int],这有效地为您提供Int s的迭代从xy),不是otherList的成员。你想要的是:

  def U(other:FinSet[T]) = {
    for(otherElem <- other.toList){
      this.+(otherElem)
    }
    this
  }

修改

好奇,鉴于你对FinSet和ListSet的定义(我在给出初步答案后才看到),你应该对上面的代码有一些其他问题(+返回{{1} },而不是FinSet,并且您没有捕获在任何地方使用List的结果,因此您的最终返回值+应该只返回集合的原始值 - 除非您不使用标准的Scala不可变List类?如果没有,你在这里使用哪个类?)。如果您使用的是标准的Scala不可变List类,那么可以考虑使用以下选项:

this

一般来说,看起来有点像你要生成你感兴趣的数据结构的可变版本会有些麻烦。我强烈建议您研究不可变数据结构以及如何使用它们 - 它们很多一旦理解了这些原则,就会更好,更安全。