标签: f-bounded-polymorphism

另一种奇怪的反复出现的模板模式

template <class Data, class Allocator = std::allocator<Node> >
class Node : public Data {
  // ...
};
Run Code Online (Sandbox Code Playgroud)

问题很简单,如何编译上面的代码?目的是为Node提供分配其他节点的可能性(以及提供默认分配器).

c++ templates f-bounded-polymorphism

3
推荐指数
2
解决办法
485
查看次数

Scala中的编译问题,具有F-bounded类型和存在类型

我正在使用F-bounded类型以便能够返回当前类型

trait Board[T <: Board[T]] {
  def updated : T
}
Run Code Online (Sandbox Code Playgroud)

我正在尝试编写一个使用它的通用辅助方法.

问题是:为什么以下不编译?

object BoardOps {
  def updated(board: Board[_]) = {
    board.updated.updated
  }
}
Run Code Online (Sandbox Code Playgroud)

错误是 value updated is not a member of _$1

我已经找到了这2个解决方法.它们是等价的吗?

object BoardOps {
  def updated(board: Board[_<:Board[_]]) = {
    board.updated.updated
  }
}

object BoardOps {
  def updated[T <: Board[T]](board: T) : T = {
    board.updated.updated
  }
}
Run Code Online (Sandbox Code Playgroud)

types type-systems scala existential-type f-bounded-polymorphism

3
推荐指数
1
解决办法
84
查看次数

奇怪的重复模板?

我有一个问题,奇怪的重复模板可以帮助很好,但我甚至无法通过一个简单的测试.

template<typename T, int _size, typename OutterT>
class Foo {

};

template<typename T>
class Bar : public Foo<T, 2, Bar> {};

//typedef Bar<float> Vec2f;


int main()
{
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这导致错误

foo.cpp:7: error: type/value mismatch at argument 3 in template parameter list for ‘template<class T, int _size, class OuterT> class Foo’
foo.cpp:7: error:   expected a type, got ‘Bar’
Run Code Online (Sandbox Code Playgroud)

我错过了什么

用g ++ 4.2.1编译

c++ templates crtp f-bounded-polymorphism

2
推荐指数
1
解决办法
270
查看次数

Scala中的递归DataType

嗨,我想知道是否有人可以解释我在Spark代码库中找到的这个签名.它看起来像一个递归数据类型,它用于构建查询计划,所以它有意义.有没有人对此有详细的了解?

abstract class TreeNode[BaseType <: TreeNode[BaseType]] extends Product 
Run Code Online (Sandbox Code Playgroud)

types scala higher-kinded-types f-bounded-polymorphism

2
推荐指数
1
解决办法
96
查看次数

F-bounded类型和方法,在参数和返回站点具有类型参数

我有一个F-bounded类型,我的目标是创建一个类型参数化方法,以便能够重用它.这是示例代码:

trait FType {
  type ThisType <: FType

  def deepCopy(): ThisType

}

class ConcreteType extends FType {

  override type ThisType = ConcreteType

  override def deepCopy(): ConcreteType = this

}

class ConcreteType2 extends FType {

  override type ThisType = ConcreteType2

  override def deepCopy(): ConcreteType2 = this

}

object FType {

  def deepCopy[T <: FType](_type: T): T = {
    _type.deepCopy()
  }

/*  def deepCopy2(c: ConcreteType2): ConcreteType2 = {
    c.deepCopy()
  }*/

  def main(args: Array[String]): Unit = {
    //deepCopy2(new ConcreteType2)
  }

}
Run Code Online (Sandbox Code Playgroud)

但是,代码无法编译.编译器抛出此错误:

Error:(29, …
Run Code Online (Sandbox Code Playgroud)

scala path-dependent-type f-bounded-polymorphism

2
推荐指数
1
解决办法
72
查看次数

Why can we use a new class as type of parent class in Scala?

In the simplified implementation of Actor in the RedBook, they use node-based MPSC node based queue for Actor. They define the node by this line of code:

private class Node[A](var a: A = null.asInstanceOf[A]) extends AtomicReference[Node[A]]
Run Code Online (Sandbox Code Playgroud)

But how can we use Node[A] as the type parameter of AtomicReference because we do not have class Node[A] yet? Is it a way of declaring recursive type in Scala?

types scala locking actor f-bounded-polymorphism

2
推荐指数
1
解决办法
65
查看次数

在 Scala 中绑定引用自身的类型意味着什么?

我在 GitHub 上查看一些代码,发现了这个类型声明:

abstract class TreeNode[BaseType <: TreeNode[BaseType]] extends Product with TreePatternBits {
Run Code Online (Sandbox Code Playgroud)

(这是来自 Spark 源代码,尽管这个问题实际上并不是关于 Spark,而是关于 Scala 的类型系统。)

这对我来说是新的。用简单的英语来说,这种类型绑定是什么意思?

generics scala f-bounded-polymorphism

2
推荐指数
1
解决办法
324
查看次数