Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在scala中扩展trait时,如何创建新的特定类型的类_Scala_Type Bounds - Fatal编程技术网

在scala中扩展trait时,如何创建新的特定类型的类

在scala中扩展trait时,如何创建新的特定类型的类,scala,type-bounds,Scala,Type Bounds,我有一个特点(除其他外)我需要一个方法来创建一个新的类实例,然后还有其他方法使用该类实例 我的代码有一个非常精简的版本: trait A { def prev: A def get(end: A): A } class B extends A { def prev: B = new B() def get(end: B): B = end.prev } 我在这里试图说明的是,next将返回类的一个新实例(实际上带有一些新的构造函数参数),get方法将在内部使用next(以及其

我有一个特点(除其他外)我需要一个方法来创建一个新的类实例,然后还有其他方法使用该类实例

我的代码有一个非常精简的版本:

trait A {
  def prev: A
  def get(end: A): A
}

class B extends A {
  def prev: B = new B()
  def get(end: B): B = end.prev
}
我在这里试图说明的是,next将返回类的一个新实例(实际上带有一些新的构造函数参数),get方法将在内部使用next(以及其他逻辑)

上面的问题是编译器说“类B必须实现抽象成员get(end:A):A”,这是合理的

我尝试使用类型边界来解决它,如下所示:

trait A {
  def prev: A
  def get(end: A): A
}

case class B extends A {
  def prev[TX <: A]: TX = new B()
  def get[TX <: A](end: TX): TX = end.prev
}
我的班级看起来像:

case class Node[T](val data: T, var prev: Node[T], var next: Node[T])

case class CircularList[T](first: Node[T], last: Node[T], current: Node[T]) 
  extends Circular[T] {

  // Nodes in the list from the current position up to but not including the end
  def toStream(end: CircularList[T]): Stream[CircularList[T]] = {
    @tailrec
    def toStreamRec(end: CircularList[T], acc: Stream[CircularList[T]]): Stream[CircularList[T]] = {
      if (this == end) {
        acc
      } else {
        toStreamRec(end.prev, Stream.cons(end.prev, acc))
      }
    }
    toStreamRec(end, Stream.empty)
  }

  def prev: CircularList[T] = new CircularList[T](first, last, current.prev)
  ...

因此,在我的简化示例中,
toStream
映射到
get

您需要的是一个名为。代码如下所示:

trait Base[T <: Base[T]] {
  def next: T
  def get(end: T): T
}

class Chlid extends Base[Child] {
  def next: Chlid = new Chlid()
  def get(end: Chlid): Chlid = end.next
}
不是对的覆盖

def get(end: A): A
因为原始方法接受类型为
A
的对象,而您的方法只需要更窄的类型
B


对于您的
循环
示例,您希望

trait Circular[T, C <: Circular[T, C]] {
  // Nodes in the list from the current position up to but NOT INCLUDING the end
  def toStream(end: C): Stream[C]

  def next: C
}

case class Node[T](val data: T, var prev: Node[T], var next: Node[T])

case class CircularList[T](first: Node[T], last: Node[T], current: Node[T]) extends Circular[T, CircularList[T]] {

  // Nodes in the list from the current position up to but not including the end
  def toStream(end: CircularList[T]): Stream[CircularList[T]] = {
    @tailrec
    def toStreamRec(end: CircularList[T], acc: Stream[CircularList[T]]): Stream[CircularList[T]] = {
      if (this == end) {
        acc
      } else {
        toStreamRec(end.prev, Stream.cons(end.prev, acc))
      }
    }

    toStreamRec(end, Stream.empty)
  }

  def prev: CircularList[T] = new CircularList[T](first, last, current.prev)

  override def next: CircularList[T] = ???
}

trait循环[T,C谢谢!这为我节省了大量时间。
def get(end: A): A
trait Circular[T, C <: Circular[T, C]] {
  // Nodes in the list from the current position up to but NOT INCLUDING the end
  def toStream(end: C): Stream[C]

  def next: C
}

case class Node[T](val data: T, var prev: Node[T], var next: Node[T])

case class CircularList[T](first: Node[T], last: Node[T], current: Node[T]) extends Circular[T, CircularList[T]] {

  // Nodes in the list from the current position up to but not including the end
  def toStream(end: CircularList[T]): Stream[CircularList[T]] = {
    @tailrec
    def toStreamRec(end: CircularList[T], acc: Stream[CircularList[T]]): Stream[CircularList[T]] = {
      if (this == end) {
        acc
      } else {
        toStreamRec(end.prev, Stream.cons(end.prev, acc))
      }
    }

    toStreamRec(end, Stream.empty)
  }

  def prev: CircularList[T] = new CircularList[T](first, last, current.prev)

  override def next: CircularList[T] = ???
}