scala中`type`和`#`关键字的调查 [英] investigation of `type` and `#` keywords in scala

查看:61
本文介绍了scala中`type`和`#`关键字的调查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能解释一下 type 关键字和 # 运算符在 Scala 中的工作原理以及如何使用它?请看例子.

Could someone explain how the type keyword and # operator works in scala and how to use it? Please look at examples.

//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

推荐答案

首先,关于type的问题:

类型声明的右侧必须是具有稳定路径的类型的名称.因此,请一一举出您的示例:

The right hand side of a type declaration has to be the name of a type with a stable path. So taking your examples one by one:

type t1 = Option.type

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

t1 is an alias for the type of the Option object, not the Option class.

type t2 = String.type

这是一个错误,因为没有 String 对象.这个错误有点奇怪,因为 String 是一个 Java 类,所以在不同的规则下运行(因为 Java 类从来没有同伴).

This is an error because there is no String object. The error's a little weird because String's a Java class and so operates under different rules (since Java classes never have companions).

type t3 = T.type

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

ditto. This time the error's clearer, because T is a Scala class and so the compiler can unambiguously say "T does not name an object with a type"

type t4 = v4.type

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

This is the singleton type of the object named by the val v4. It doesn't refer to any instance of type T, or even any instance of the anonymous class created by your new T{} expression. It refers to a type that is only represented by v4 and null, i.e. they are the only allowed values of that type.

type t4_1 = (new T{}).type

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

This is illegal because the thing you're taking the type of has to be a stable identifier (roughly, an identifier whose referant can never change -- if the full path to the idenfier consists of only the names of packages, objects, and vals, it's stable).

type t5 = "abc".type

同上.

type t6 = v6.type

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

v6 is a stable identifier. t6 is the type inhabited solely by that particular instance of String which is referred to by the name v6 (and null).

type v6 = v1_1.type

还是单例类型.

val v7: t7 = null

nullt7

val v7_1:t7 = v1_1

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

So is this particular object.

val v7_2:t7 = Some(10)

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

But this is a different object (even though it's == to v7, it's not eq to it) and therefore is not a member of this type.

现在关于#:

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

AB 是类型参数.他们不能在课堂外被引用.您可以将它们视为具有 private[this] 可见性的抽象类型别名,尽管这不太准确.

A and B are type parameters. They can't be referred to outside the class. You can think of them like abstract type aliases with private[this] visibility, though that's not quite accurate.

type xa = X[A,B]#A

所以是的,不可见.

type xa2_1 = X2[String]#A

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

Since this A refers to a public type alias, it can be referred to by name outside the class. Note that this particular case is pretty useless, because you know absolutely nothing about this type. if your trait X2 had a method that returned values of type A, you could do something like

val aFromX2: xa2_1 = x2instance.methodThatReturnsAnA

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

..but then you couldn't do anything else with it, even pass it back to an instance of X2[String] because there's no guarantee that that the two As would refer to the same type! On the other hand, if you have a concrete instance, you could do this:

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

在这种情况下它有效,因为即使我们不知道 A 实际上是什么,我们知道这两种方法使用 相同 类型——无论是固定在x2instance 的构造.

In this case it works because even though we have no idea what A actually is, we know that the two methods use the same type -- whatever was fixed at x2instance's construction.

这篇关于scala中`type`和`#`关键字的调查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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