Scala 特性和 C++ 之间的区别概念

发布于 2024-09-06 11:24:36 字数 400 浏览 5 评论 0原文

Scala 特征 Haskell 类型类和 C++0x 概念之间有什么区别?

就像下面这个例子,观察者声明一个抽象成员 receiveUpdate 观察者实际上是一种“匿名”类型或结构类型。

package observer
trait Subject {
   type Observer = { def receiveUpdate(subject: Any) }
   private var observers = List[Observer]()
   def addObserver(observer:Observer) = observers ::= observer
   def notifyObservers = observers foreach (_.receiveUpdate(this))
}

What is the difference between Scala traits Haskell type class and C++0x Concepts?

Like in this example below where Observer declare an abstract members receiveUpdate
Observer is in fact a “anonymous” types or Structural types.

package observer
trait Subject {
   type Observer = { def receiveUpdate(subject: Any) }
   private var observers = List[Observer]()
   def addObserver(observer:Observer) = observers ::= observer
   def notifyObservers = observers foreach (_.receiveUpdate(this))
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

赏烟花じ飞满天 2024-09-13 11:24:36
  • 这些概念不会出现在 C++0x 中,它们已在规范草案的最后几个版本中被删除。
  • 类型类最初设计用于参数多态性的有界量化(通用约束,“forall x,使得 x 是...”)以及一种以不太特别的方式为完全类型推断语言提供特别多态性的机制方式。
  • 概念还用于参数多态性的有界量化,并提供概念重载,这弥补了 C++ 中模板函数部分专业化的不足。它们最初是为了处理 C++ 中的模板错误消息问题而设计的。
  • 特征是在不使用多重继承的情况下混合行为的机制。

因此,它们中只有两个有一些共同点,但不是很多,那就是概念和类型类。本文已经对两者进行了比较:C++ 概念的比较和 Haskell 类型类

  • Concepts are not coming to C++0x, they've been removed in the last few versions of the draft spec.
  • Type-classes where originally designed for bounded quantification of parametric polymorphism (generic constraints, "forall x such that x is an...") and a mechanism to provide ad-hoc polymorphism for a totally type inferred language in a less ad-hoc manner.
  • Concepts are also for bounded quantification of parametric polymorphism and provide concept overloading, which makes up for the lack of partial specializations of template functions in C++. They where originally designed to deal with template error message problem in C++.
  • Traits are mechanism to mix-in behaviors without using multiple-inheritance.

So only two of them have something in common but not by much, that is Concepts and Type-classes. There already has been comparisons between the two in this paper: A comparison of C++ concepts and Haskell type classes

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文