Scala 编译器有哪些特殊类型? [英] What types are special to the Scala compiler?

查看:40
本文介绍了Scala 编译器有哪些特殊类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Scala 在如何将语言特性实现为库特性方面做了大量工作.

Scala makes a big deal about how what seem to be language features are implemented as library features.

是否有语言特别处理的类型列表?

Is there a list of types that are treated specially by the language?

是在规范中还是作为实现细节?

Either in the specification or as an implementation detail?

例如,这将包括优化元组上的匹配.

That would include, for example, optimizing away matches on tuples.

与模式匹配、推导式、try-catch 块和其他语言结构相关的特殊约定如何?

What about special conventions related to pattern matching, for comprehensions, try-catch blocks and other language constructs?

String 对编译器来说有什么特殊之处吗?我看到字符串增强只是一个库隐式转换,并且 Predef 支持字符串连接,但这是否是语言的特殊情况?

Is String somehow special to the compiler? I see that String enhancement is just a library implicit conversion, and that String concatenation is supported by Predef, but is that somehow special-cased by the language?

同样,我看到关于 <:<classOfasInstanceOf 的问题,不清楚什么是神奇的内在函数.有没有办法通过编译器选项或查看字节码来区分差异?

Similarly, I see questions about <:< and classOf and asInstanceOf, and it's not clear what is a magical intrinsic. Is there a way to tell the difference, either with a compiler option or by looking at byte code?

我想了解一个功能是否被 Scala.JS 和 Scala-native 等实现统一支持,或者一个功能是否实际上可能被证明是依赖于实现的,具体取决于库实现.

I would like to understand if a feature is supported uniformly by implementations such as Scala.JS and Scala-native, or if a feature might actually prove to be implementation-dependent, depending on the library implementation.

推荐答案

编译器已知"的类型数量惊人,并且在不同程度上具有特殊性.您可以在 scalac 的 定义中找到完整列表.Scala.

There is an incredible amount of types that are "known" of the compiler, and are special to varying degrees. You can find a complete list in scalac's Definitions.scala.

我们大概可以根据它们的特殊程度对它们进行分类.

We can probably classify them according to the degree of specialness they bear.

免责声明:我可能还忘记了一些.

Disclaimer: I have probably forgotten a few more.

以下类型对 Scala 的类型系统至关重要.它们会影响类型检查本身的执行方式.所有这些类型都在规范中提到(或者至少,他们绝对应该提到).

The following types are crucial to Scala's type system. They have an influence on how type checking itself is performed. All these types are mentioned in the specification (or at least, they definitely should be).

  • AnyAnyRefAnyValNullNothing:五个位于 Scala 类型系统顶部和底部的类型.
  • scala.FunctionN,匿名函数(包括 eta 扩展)的(规范)类型.即使在对匿名函数进行 SAM 处理的 2.12 中,FunctionN 在某些情况下(特别是在重载解析中)仍然很特殊.
  • scala.PartialFunction(对类型推断的工作方式有影响)
  • 单位
  • 具有文字符号的所有类型:IntLongFloatDoubleCharBooleanStringSymboljava.lang.Class
  • 所有数字原始类型和Char,用于弱一致性(这两个项目符号一起涵盖所有原始类型)
  • Option 和元组(用于模式匹配和自动元组)
  • java.lang.Throwable
  • scala.Dynamic
  • scala.Singleton
  • 大部分scala.reflect.*,特别是ClassTagTypeTag
  • scala.annotation.{,ClassFile,Static}注解
  • scala.annotation.* 中几乎所有的注解(例如,unchecked)
  • scala.language.*
  • scala.math.ScalaNumber(原因不明)
  • Any, AnyRef, AnyVal, Null, Nothing: the five types that sit at the top and bottom of Scala's type system.
  • scala.FunctionN, the (canonical) type given of anonymous functions (including eta-expansion). Even in 2.12, which has the SAM treatment of anonymous functions, FunctionN remains special in some cases (notably in overloading resolution).
  • scala.PartialFunction (has an impact on how type inference works)
  • Unit
  • All types with literal notation: Int, Long, Float, Double, Char, Boolean, String, Symbol, java.lang.Class
  • All numeric primitive types and Chars, for weak conformance (together, these two bullets cover all primitive types)
  • Option and tuples (for pattern matching and auto-tupling)
  • java.lang.Throwable
  • scala.Dynamic
  • scala.Singleton
  • Most of scala.reflect.*, in particular ClassTag, TypeTag, etc.
  • scala.annotation.{,ClassFile,Static}Annotation
  • Almost all of the annotations in scala.annotation.* (e.g., unchecked)
  • scala.language.*
  • scala.math.ScalaNumber (for obscure reasons)

以下类型对类型系统并不重要.它们对类型检查没有影响.但是,Scala 语言确实具有许多结构,可以将这些结构脱糖为这些类型的表达式.

The following types are not crucial to the type system. They do not have an influence on type checking. However, the Scala language does feature a number of constructs which desugar into expressions of those types.

规范中也会提到这些类型.

These types would also be mentioned in the specification.

  • scala.collection.SeqNilWrappedArray,用于可变参数.
  • TupleN 类型
  • ProductSerializable(用于案例类)
  • MatchError,由模式匹配结构生成
  • scala.xml.*
  • scala.DelayedInit
  • List(编译器对它们进行一些微不足道的优化,例如将 List() 重写为 Nil)
  • scala.collection.Seq, Nil and WrappedArray, which are used for varargs parameters.
  • TupleN types
  • Product and Serializable (for case classes)
  • MatchError, generated by pattern matching constructs
  • scala.xml.*
  • scala.DelayedInit
  • List (the compiler performs some trivial optimizations on those, such as rewriting List() as Nil)

这可能是您最关心的列表,因为您说您有兴趣了解不同后端可能会有什么不同.前面的类别由编译器的早期(前端)阶段处理,因此由 Scala/JVM、Scala.js 和 Scala Native 共享.这个类别通常是编译器后端已知的,因此可能有不同的处理方式.请注意,Scala.js 和 Scala Native 都尝试在合理程度上模仿 Scala/JVM 的语义.

This is probably the list that you care most about, given that you said you were interested in knowing what could go differently on different back-ends. The previous categories are handled by early (front-end) phases of the compiler, and are therefore shared by Scala/JVM, Scala.js and Scala Native. This category is typically known of the compiler back-end, and so potentially have different treatments. Note that both Scala.js and Scala Native do try to mimic the semantics of Scala/JVM to a reasonable degree.

语言规范本身可能没有提到这些类型,至少不是全部.

Those types might not be mentioned in the language specification per se, at least not all of them.

以下是后端同意的内容(据我所知,重新使用 Scala Native):

Here are those where the back-ends agree (re Scala Native, to the best of my knowledge):

  • 所有原始类型:BooleanCharByteShortIntLongFloatDoubleUnit.
  • scala.Array.
  • Cloneable(目前在 Scala Native 中不受支持,请参阅 #334)
  • StringStringBuilder(主要用于字符串连接)
  • Object,几乎所有的方法
  • All primitive types: Boolean, Char, Byte, Short, Int, Long, Float, Double, Unit.
  • scala.Array.
  • Cloneable (currently not supported in Scala Native, see #334)
  • String and StringBuilder (mostly for string concatenation)
  • Object, for virtually all its methods

以下是他们不同意的地方:

And here are those where they disagree:

  • 原始类型的盒装版本(例如 java.lang.Integer)
  • 可序列化
  • java.rmi.Remotejava.rmi.RemoteException
  • scala.annotation.* 中的一些注解(例如,strictfp)
  • java.lang.reflect.*中的一些东西,Scala/JVM用来实现结构类型
  • Boxed versions of primitive types (such as java.lang.Integer)
  • Serializable
  • java.rmi.Remote and java.rmi.RemoteException
  • Some the annotations in scala.annotation.* (e.g., strictfp)
  • Some stuff in java.lang.reflect.*, used by Scala/JVM to implement structural types

另外,虽然不是类型本身,但是一长串原始方法 也由后端专门处理.

Also, although not types per se, but a long list of primitive methods are also handled specifically by the back-ends.

除了上述所有平台上都可用的类型之外,非 JVM 平台还添加了自己的特殊类型以实现互操作性.

In addition to the types mentioned above, which are available on all platforms, non-JVM platforms add their own special types for interoperability purposes.

参见 JSDefinitions.scala

  • js.Any:概念上是 Any 的第三个子类型,除了 AnyValAnyRef.它们具有 JavaScript 语义而不是 Scala 语义.
  • String 和所有原始类型的盒装版本(被编译器大量重写——所谓的劫持")
  • js.ThisFunctionN:它们的 apply 方法与其他 JavaScript 类型的行为不同(第一个实际参数成为被调用的 thisArgument函数)
  • js.UndefOrjs.|(它们的行为就像 JS 类型,即使它们没有扩展 js.Any)
  • js.Object(new js.Object() 是一个特殊的空 JS 对象字面量 {})
  • js.JavaScriptException(在 throwcatch 中表现非常特殊)
  • js.WrappedArray(用于可变参数的脱糖)
  • js.ConstructorTag(类似于ClassTag)
  • 注解js.native,以及js.annotation.*
  • 中的所有注解
  • js.Any: conceptually a third subtype of Any, besides AnyVal and AnyRef. They have JavaScript semantics instead of Scala semantics.
  • String and the boxed versions of all primitive types (heavily rewritten--so-called "hijacked"--by the compiler)
  • js.ThisFunctionN: their apply methods behave differently than that of other JavaScript types (the first actual argument becomes the thisArgument of the called function)
  • js.UndefOr and js.| (they behave as JS types even though they do not extend js.Any)
  • js.Object (new js.Object() is special-cased as an empty JS object literal {})
  • js.JavaScriptException (behaves very specially in throw and catch)
  • js.WrappedArray (used by the desugaring of varargs)
  • js.ConstructorTag (similar to ClassTag)
  • The annotation js.native, and all annotations in js.annotation.*

另外还有十几个原始方法.

请参阅 斯卡拉

  • 无符号整数:UByteUShortUIntULong
  • Ptr,指针类型
  • FunctionPtrN,函数指针类型
  • native.*
  • 中的注解
  • scala.scalanative.runtime 中的一些额外的原始方法
  • Unsigned integers: UByte, UShort, UInt and ULong
  • Ptr, the pointer type
  • FunctionPtrN, the function pointer types
  • The annotations in native.*
  • A number of extra primitive methods in scala.scalanative.runtime

这篇关于Scala 编译器有哪些特殊类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆