scala - 类型成员为 : what is Scala's object vs module system ? 的对象(试图理解 2014 年 Odersky 关于路径相关类型的论文)

标签 scala module path-dependent-type type-members

我正在阅读 Foundations of path dependent types .在第一页的右栏中,写着:

Our motivation is twofold. First, we believe objects with type members are not fully understood. It is not clear what causes the complexity, which pieces of complexity are essential to the concept or accidental to a language implementation or calculus that tries to achieve something else. Second, we believe objects with type members are really useful. They can encode a variety of other, usually separate type system features. Most importantly, they unify concepts from object and module systems, by adding a notion of nominality to otherwise structural systems.



有人可以澄清/解释“对象与模块”系统是什么意思吗?

或者一般来说,什么

"they (objects with type members) unify concepts from object and module systems, by adding a notion of nominality to otherwise structural systems."



意思是 ?

什么概念?从哪里 ?

对象名称/值的名义性?
类型中的结构 ?还是反过来?

这里的类型成员属于哪里?到模块系统?对象系统 ?如何?为什么?

编辑:

这种统一与路径相关类型有何关系?在我看来,他们允许这种统一发生(具有类型成员的对象)。是这样吗 ?
如果是,如何?

你能举一个简单的例子这意味着什么吗? (即允许模块和对象系统统一的路径依赖类型与如果我们没有路径依赖类型,为什么统一不可能发生?)

编辑2:

从论文中:

To make any use of type members, programmers need a way to refer to them. This means that types must be able to refer to objects, i.e. contain terms that serve as static approximation of a set of dynamic objects. In other words, some level of dependent types is required; the usual notion is that of path-dependent types.



所以到目前为止我的理解(在杰斯珀的回答的帮助下):

上面这段部分回答了上面的一些问题。主要似乎是拥有具有类型成员的对象并需要具有依赖于路径的类型,因为对象是动态/运行时依赖的,但类型是静态的(在编译时定义)所以仅仅拥有导致类型成员的对象是行不通的因为那时那些类型成员不会在编译时明确定义。

路径相关类型通过在编译时确定通向类型成员的路径(通过要求在编译时已知/定义对象)来提供帮助,因此即使路径通过对象(在编译时可能会更改)但是如果这些对象在编译时已经固定,那么它们的类型成员在编译时也可以具有明确的含义。

最佳答案

我不确定我是否完全理解你的问题,但我会试一试。 :) 我认为作者主要指的是ML style modules其中签名对应于 Scala 特征,结构对应于 Scala 对象。 Scala 统一了记录值、对象和模块的概念,这些概念在大多数其他语言(如 ML、Rust 等)中是独立的概念。主要好处是,在 Scala 中,模块/对象可以作为普通函数参数传递(而在 ML 中,您必须为此使用特殊的仿函数)。

在 ML 中,基于其结构(类似于 Scala 中的结构类型)检查模块与签名(Scala 中的特征)的兼容性,但在 Scala 中,模块必须通过名称(名义类型)实现特征。因此,即使两个模块/对象在 Scala 中具有相同的结构,它们也可能不兼容,具体取决于它们的父类(super class)型层次结构。

Scala 中关于类型成员的一个非常强大的特性是,即使您不知道其类型成员的确切类型,只要您以类型安全的方式执行它(我认为这在 ML 中也是可能的),您也可以使用它模块),例如:

trait A {
  type X
  def getX: X
  def setX(x: X): Unit
}

def foo(a: A) = a.setX(a.getX)

foo Scala 编译器不知道 a.X 的确切类型但是类型的值仍然可以以编译器知道是安全的方式使用。这是不可能的in Rust例如。

Scala 编译器的下一个版本 Dotty 将基于您引用的论文中描述的理论。模块和对象与子类型、特征和类型成员相结合的这种统一是 Scala 独特且非常强大的原因之一。

编辑:为了稍微扩展一下为什么依赖路径的类型会增加 Scala 模块/对象系统的灵 active ,让我们扩展上面的示例:
def bar(a: A, b: A) = a.setX(b.getX)

这将导致编译错误:
error: type mismatch;
 found   : b.T
 required: a.T
  def foo(a: A, b: A) = a.setX(b.getX)
                                ^

正确,因为a.Tb.T可以解析为不同的类型。您可以通过使用路径相关类型来修复它:
def bar(a: A)(b: A { type X = a.X }) = a.setX(b.getX)

或者添加一个类型参数:
def bar[T](a: A { type X = T }, b: A { type X = T }) = a.setX(b.getX)

因此,路径相关类型消除了对类型参数的一些需求,并且还允许我们有效地表达存在类型(对应于 A[_]A[T] forSome { type T } 如果 A 有类型参数而不是类型成员)。

关于scala - 类型成员为 : what is Scala's object vs module system ? 的对象(试图理解 2014 年 Odersky 关于路径相关类型的论文),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47098538/

相关文章:

c++ - Scala 和 C++11 的类型推断有什么区别?

python - 重新加载已导入到另一个模块的模块

ruby-on-rails - Rails 模块中的 mattr_accessor 是什么?

scala - `forSome { val ` 的样本?

scala - 将 scalac 插件拆分为多个文件

Scala 模式匹配区分路径依赖类型取决于范围实例定义在?

scala - 如何在Scala中将类方法作为参数传递

scala - 将属性添加到类型而不是类型实例的隐式转换

scala - 为什么 from_json 失败并显示 “not found : value from_json"? (2)

javascript - 页面中有多个js文件,每个文件都以require() : bad practice or OK?开头