调查scala中的`type`和`#`关键字

paw*_*icz 11 types scala type-projection

有人可以解释type关键字和#运算符在scala中的工作方式以及如何使用它吗?请看一些例子.

//Example1
scala>  type t1 = Option.type
defined type alias t1

//Shouldn't this work since previous example simply works?
scala>  type t2 = String.type
<console>:7: error: type mismatch;
 found   : String.type
 required: AnyRef
       type t2 = String.type
         ^

//lets define custom trait T
scala>  trait T
defined trait T

//... and obtain it's type like in Example1.
//Shouldn't this work since previous Example1 simply works?
scala>  type t3 = T.type
<console>:7: error: not found: value T
       type t3 = T.type
         ^

//Lets define some value of type T
scala>  val v4 = new T{}
v4: T = $anon$1@5c3e8c76

//and obtain it's type (works)
scala>  type t4 = v4.type
defined type alias t4

//this doesn't work
scala>  type t4_1 = (new T{}).type
<console>:1: error: identifier expected but 'new' found.
       type t4_1 = (new T{}).type

//as well as this (doesn't work)
scala>  type t5 = "abc".type
<console>:1: error: identifier expected but string literal found.
       type t5 = "abc".type
         ^

//but this compiles well
scala>  val v6 = "abc"
v6: String = abc

scala>  type t6 = v6.type
defined type alias t6


//lets create some values of created types:
scala>  type t1 = Option.type
defined type alias t1

scala>  val v1_1 = Some(10)
v1_1: Some[Int] = Some(10)

scala>  type t7 = v1_1.type
defined type alias t7

scala>  val v7:t7 = null
v7: t7 = null

scala>  val v7_1:t7 = v1_1
v7_1: t7 = Some(10)

scala>  val v7_2:t7 = Some(10)
<console>:9: error: type mismatch;
 found   : Some[Int]
 required: t7
    (which expands to)  v1_1.type
       val v7_2:t7 = Some(10)
             ^


//next let's try # operator

scala>  class X[A,B](a:A,b:B)
defined class X

//doesn't work
scala>  type xa = X[A,B]#A
<console>:8: error: not found: type A
       type xa = X[A,B]#A
           ^
<console>:8: error: not found: type B
       type xa = X[A,B]#A
             ^

//but such approach works:
scala>  trait X2[C]{
  type A
  type B
  val c:C
}
defined trait X2

scala>  type xa2_1 = X2[String]#A
defined type alias xa2_1

scala>  type xa2_2[M] = X2[M]#A
defined type alias xa2_2
Run Code Online (Sandbox Code Playgroud)

RM.*_*RM. 14

首先,您的问题type:

类型声明的右侧必须是具有稳定路径的类型的名称.所以逐个举例:

type t1 = Option.type
Run Code Online (Sandbox Code Playgroud)

t1是Option 对象类型的别名,而不是Option类.

type t2 = String.type
Run Code Online (Sandbox Code Playgroud)

这是一个错误,因为没有String对象.错误有点奇怪,因为String是一个Java类,因此在不同的规则下运行(因为Java类永远不会有伴侣).

type t3 = T.type
Run Code Online (Sandbox Code Playgroud)

同上.这次错误更清楚,因为T是一个Scala类,因此编译器可以明确地说"T没有用类型命名对象"

type t4 = v4.type
Run Code Online (Sandbox Code Playgroud)

这是val命名的对象的单例类型v4.它不引用任何类型为T的实例,甚至不引用由new T{}表达式创建的任何匿名类实例.它指的是v4和表示的类型null,即它们是该类型的唯一允许值.

type t4_1 = (new T{}).type
Run Code Online (Sandbox Code Playgroud)

这是非法的,因为你所采用的类型必须是一个稳定的标识符(粗略地说,一个标识符,其参数永远不会改变 - 如果标识符的完整路径只包含包,objects和vals的名称) ,它很稳定).

type t5 = "abc".type
Run Code Online (Sandbox Code Playgroud)

同上.

type t6 = v6.type
Run Code Online (Sandbox Code Playgroud)

v6是一个稳定的标识符. t6是仅由名称v6(和null)引用的特定String实例居住的类型.

type v6 = v1_1.type
Run Code Online (Sandbox Code Playgroud)

再次,单身类型.

val v7: t7 = null
Run Code Online (Sandbox Code Playgroud)

null 是类型的有效值 t7

val v7_1:t7 = v1_1
Run Code Online (Sandbox Code Playgroud)

这个特定的对象也是如此.

val v7_2:t7 = Some(10)
Run Code Online (Sandbox Code Playgroud)

但是,这是一个不同的对象(即使它==v7,它不是eq的话),因此不在此类型的成员.

现在关于#:

class X[A,B](a:A,b:B)
Run Code Online (Sandbox Code Playgroud)

A并且B是类型参数.他们不能在课外提到.您可以将它们视为具有private[this]可见性的抽象类型别名,尽管这不太准确.

type xa = X[A,B]#A
Run Code Online (Sandbox Code Playgroud)

所以,是的,不可见.

type xa2_1 = X2[String]#A
Run Code Online (Sandbox Code Playgroud)

由于 A是指公共类型别名,因此可以在类外部通过名称引用它.请注意,这种特殊情况非常无用,因为您对此类型一无所知.如果你的特征X2有一个返回类型值的方法A,你可以做类似的事情

val aFromX2: xa2_1 = x2instance.methodThatReturnsAnA
Run Code Online (Sandbox Code Playgroud)

..但是你不能用它做任何其他事情,甚至把它传回一个实例,X2[String]因为不能保证两个As会引用相同的类型!另一方面,如果你有一个具体的实例,你可以这样做:

def passAroundA(x2instance: X2[String]) {
  type x2a = x2instance.A // note dot, not #
  val a: x2a = x2instance.methodThatReturnsAnA
  x2instance.methodThatTakesAnA(a)
}
Run Code Online (Sandbox Code Playgroud)

在这种情况下它起作用,因为即使我们不知道究竟A是什么,我们知道这两种方法使用相同的类型 - 无论在x2instance构造中修复了什么.