Scala 的 .type 和 Java 的 .class 文字

发布于 2024-11-09 05:39:45 字数 188 浏览 3 评论 0原文

我想从语言设计的角度来看为什么Scala删除了Java的类文字(例如String.class)并用classOf[String]替换它,但随后添加了一个“类型”文字”及其单例如 Singleton.type 而不是类似 typeOf[Singleton]

I wonder from a language design perspective why Scala has removed Java's class literal (e. g. String.class) and replaced it with classOf[String], but has then added a "type literal" with its Singletons like Singleton.type instead of something like typeOf[Singleton]?

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

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

发布评论

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

评论(2

桃酥萝莉 2024-11-16 05:39:45

这是我的合理化:

classOf[T]

classOfPredef 中定义为具有以下签名的函数:

def classOf[T]: Class[T]

虽然它是由编译器实现的,但使用函数语法是可能的,无需在语法方面进行任何特殊处理。这就是考虑此选项的原因之一。

String.class 这样的替代方案意味着每个类都有一个带有字段 class 的伴生对象。所以有两个问题:

  1. 语法需要特殊情况。
  2. class 是一个关键字,因此会导致一个问题,如果您只创建 class A ,则 一个伴生对象,能够引用 A.class 会很奇怪,这就像访问伴生对象 上的 class 字段一样一个。

A.type:

为什么typeOf[A]可能会令人困惑。它看起来像函数调用,但类型与函数结果并不存在于同一个世界中(函数结果具有类型,但类型本身仅在编译时才有意义)。我可以将类型归因于变量:

scala> val a: A.type = A
a: A.type = A$@c21a68

我无法像函数返回的那样分配类型:

scala> val b = A.type
<console>:1: error: identifier expected but 'type' found.
   val b = A.type
             ^

另一方面,类型可以是对象的成员:

scala> object A { type type1 = Int }
defined module A

scala> val x: A.type1 = 1
x: A.type1 = 1

因此拥有 A.type 并不困难 指对象A 的类型。请注意,除了引用单例对象的类型之外,.type 不被使用,因此它并不是那么频繁。

Here is my rationalization:

classOf[T]

classOf is defined in Predef as a function with this signature:

def classOf[T]: Class[T]

Although it's implemented by the compiler, using the function syntax is possible without having to create any special treatment in terms of syntax. So that's one reason here to consider this option.

The alternative like String.class would imply that each class has a companion object with a field class. So there are two problems:

  1. class is a keyword, so that causes a problem where the syntax would require a special case for it
  2. if you just create class A without a companion object, it's would be odd to be able to refer to A.class, which would be like accessing the class field on the companion A.

A.type:

On why typeOf[A] may be confusing. It looks like a function call, but types don't live in the same world as function results (function results have types, but the type itself only makes sense at compile time). I can ascribe a type to a variable:

scala> val a: A.type = A
a: A.type = A$@c21a68

I can't assign a type like it's returned by a function:

scala> val b = A.type
<console>:1: error: identifier expected but 'type' found.
   val b = A.type
             ^

On the other hand types can be member of a object:

scala> object A { type type1 = Int }
defined module A

scala> val x: A.type1 = 1
x: A.type1 = 1

So it is not a big stretch to have A.type refer to the type of object A. Note that .type aren't used beyond referring to types of singleton objects, so it's not really that frequent.

二智少女 2024-11-16 05:39:45

事实上,这是非常一致的。 Singleton.typeSingleton 的依赖类型,而 classOf[Class] 是方法的类型参数。

考虑一下:

class A {
    class B
}

val a: A = new A
val b: a.B = new a.B

这里的要点是 . 用于指示属于值成员的内容。它可以是 valvardefobject,也可以是 >类型特征

由于单例对象是一个值,因此 Singleton.type 是完全有效的。

另一方面,类不是对象,因此Class.class没有意义。 Class 不存在(作为值),因此不可能获取它的成员。另一方面,它的定义为 def classOf[T]: Class[T] 是纯 Scala 代码(即使实际实现是编译器魔法)。

Actually, it is quite consistent. Singleton.type is a dependent type of Singleton, while classOf[Class] is a type parameter to a method.

Consider this:

class A {
    class B
}

val a: A = new A
val b: a.B = new a.B

The point here is that . is used to indicate something that is a member of a value. It may be a val, a var, a def or an object and it may also be a type, a class or a trait.

Since a singleton object is a value, then Singleton.type is perfectly valid.

On the other hand, a class is not an object, so Class.class doesn't make sense. Class doesn't exist (as a value), so it is not possible to get a member of it. On the other hand, it's definition as def classOf[T]: Class[T] is plain Scala code (even if the actual implementation is compiler magic).

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