Scala 的 .type 和 Java 的 .class 文字
我想从语言设计的角度来看为什么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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是我的合理化:
classOf[T]
classOf
在Predef
中定义为具有以下签名的函数:虽然它是由编译器实现的,但使用函数语法是可能的,无需在语法方面进行任何特殊处理。这就是考虑此选项的原因之一。
像
String.class
这样的替代方案意味着每个类都有一个带有字段class
的伴生对象。所以有两个问题:class
是一个关键字,因此会导致一个问题,如果您只创建class A
,则 一个伴生对象,能够引用A.class
会很奇怪,这就像访问伴生对象上的 class 字段一样一个。
A.type:
为什么
typeOf[A]
可能会令人困惑。它看起来像函数调用,但类型与函数结果并不存在于同一个世界中(函数结果具有类型,但类型本身仅在编译时才有意义)。我可以将类型归因于变量:我无法像函数返回的那样分配类型:
另一方面,类型可以是对象的成员:
因此拥有
A.type 并不困难
指对象A
的类型。请注意,除了引用单例对象的类型之外,.type
不被使用,因此它并不是那么频繁。Here is my rationalization:
classOf[T]
classOf
is defined inPredef
as a function with this signature: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 fieldclass
. So there are two problems:class
is a keyword, so that causes a problem where the syntax would require a special case for itclass A
without a companion object, it's would be odd to be able to refer toA.class
, which would be like accessing the class field on the companionA
.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:I can't assign a type like it's returned by a function:
On the other hand types can be member of a object:
So it is not a big stretch to have
A.type
refer to the type of objectA
. Note that.type
aren't used beyond referring to types of singleton objects, so it's not really that frequent.事实上,这是非常一致的。
Singleton.type
是Singleton
的依赖类型,而classOf[Class]
是方法的类型参数。考虑一下:
这里的要点是
.
用于指示属于值成员的内容。它可以是val
、var
、def
或object
,也可以是>类型
、类
或特征
。由于单例对象是一个值,因此
Singleton.type
是完全有效的。另一方面,类不是对象,因此
Class.class
没有意义。Class
不存在(作为值),因此不可能获取它的成员。另一方面,它的定义为def classOf[T]: Class[T]
是纯 Scala 代码(即使实际实现是编译器魔法)。Actually, it is quite consistent.
Singleton.type
is a dependent type ofSingleton
, whileclassOf[Class]
is a type parameter to a method.Consider this:
The point here is that
.
is used to indicate something that is a member of a value. It may be aval
, avar
, adef
or anobject
and it may also be atype
, aclass
or atrait
.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 asdef classOf[T]: Class[T]
is plain Scala code (even if the actual implementation is compiler magic).