简体   繁体   English

存在类型和类型成员

[英]Existential types and type members

What works (Part A) 什么有效(A部分​​)

Suppose I have a trait with a type parameter: 假设我有一个带有类型参数的特征:

trait A[T]

I can use an existential type to write a method that will take a collection of A s that all have the same T : 我可以使用存在类型来编写一个方法,该方法将采用所有具有相同TA的集合:

def foo(as: Seq[A[X]] forSome { type X }) = true

Note that this is different from the following: 请注意,这与以下内容不同:

def otherFoo(as: Seq[A[X] forSome { type X }]) = true

Or the equivalent: 或等效的:

def otherFoo(as: Seq[A[_]]) = true

In these cases the scope of the existential is inside the Seq , so the A s can have different T s. 在这些情况下,存在体的范围在Seq内部,因此A s可以具有不同的T s。 With my original foo (with the existential scoping over the Seq ), the following is fine: 使用我原来的foo (对Seq的存在范围),以下情况很好:

foo(Seq(new A[Int] {}, new A[Int] {}))

But make the type parameters different and it doesn't compile: 但是使类型参数不同并且不能编译:

scala> foo(Seq(new A[Int] {}, new A[String] {}))
<console>:10: error: type mismatch;
 found   : Seq[A[_ >: java.lang.String with Int]]
 required: Seq[A[X]] forSome { type X }

              foo(Seq(new A[Int] {}, new A[String] {}))
                     ^

This is all pretty straightforward. 这一切都非常简单。

What works (Part B) 什么有效(B部分)

Now suppose I have a similar trait with a type member instead of a type parameter: 现在假设我有类似成员的类似特征而不是类型参数:

trait B { type T }

I can write a method that will only take a B with some specified T : 我可以编写一个只带有一些指定TB的方法:

scala> def bar[X](b: B { type T = X }) = true
bar: [X](b: B{type T = X})Boolean

scala> bar[Int](new B { type T = Int })
res5: Boolean = true

scala> bar[String](new B { type T = Int })
<console>:10: error: type mismatch;
 found   : java.lang.Object with B
 required: B{type T = String}
              bar[String](new B { type T = Int })
                          ^

Again, this works exactly the way you'd expect it to. 同样,这完全符合您的预期。

What doesn't work 什么行不通

When we try to write the equivalent of our foo above, but for type members, things get weird. 当我们尝试编写上述foo的等价物,但对于类型成员,事情变得奇怪。

scala> def baz(bs: Seq[B { type T = X }] forSome { type X }) = true
baz: (as: Seq[B{type T = X}] forSome { type X })Boolean

scala> baz(Seq(new B { type T = Int }, new B { type T = String }))
res7: Boolean = true

That the last line compiles makes no sense to me. 最后一行编译对我来说毫无意义。 I've told it that I want all the type members to be the same. 我告诉它我希望所有类型的成员都是一样的。 My foo shows that I can do this for type parameters, and bar shows that I can constrain a type based on its type members. 我的foo表明我可以为类型参数执行此操作, bar显示我可以基于其类型成员约束类型。 But I can't combine the two. 但我无法将两者结合起来。

I've tried this on 2.9.2 and 2.10.0-M5. 我在2.9.2和2.10.0-M5上试过这个。

Motivation 动机

This question is inspired by this one , where my first thought was, oh, just use an existential type (setting aside for a second the issue that it seems to be impossible to get an existential type to scope of the type of a repeated parameter , which would be handy here): 这个问题的灵感来自于这个问题,我的第一个想法是,哦,只使用存在类型(暂时搁置一个问题, 似乎不可能将存在类型转换为重复参数类型的范围 ,这将是方便的):

def accept(rs: Seq[RList[Int] { type S = X }] forSome { type X }) = true

But this doesn't actually work—you get the same weird result as in the simplified example above. 但这实际上并不起作用 - 你得到了与上面简化例子中相同的奇怪结果。

I've finally sort it out (at least I hope so). 我终于解决了(至少我希望如此)。 Let's do it the other way. 让我们以另一种方式做。 We build our trait: 我们建立我们的特质:

scala> trait B {type T}
defined trait B

We try and build a sequence of B : 我们尝试构建一个B序列:

scala> Seq(new B {type T = Int}, new B {type T = String})
res0: Seq[B{type T >: String with Int}] = List($anon$1@592b12d, $anon$2@61ae0436)

Damn, it works! 该死的,它的作品! Ok, we don't have an equality for type T but let's play with it: 好吧,我们没有type T的相等,但让我们玩它:

scala> res0 : (Seq[B {type T = X}] forSome {type X >: String with Int})
res1: Seq[B{type T = X}] forSome { type X >: String with Int } = List($anon$1@592b12d, $anon$2@61ae0436)

It's closer. 它更接近了。 No wait, it's not closer, It's better than what you've proposed as a parameter of baz , we do not only provide a raw type, we also have a upper bound! 没有等待,它不是更接近,它比你提出的baz参数更好 ,我们不仅提供原始类型,我们也有一个上限! Thus, we can clearly pass it to baz . 因此,我们可以清楚地将它传递给baz This is why it doesn't work as you expected. 这就是它无法按预期工作的原因。

In your example : 在你的例子中:

scala> def baz(bs: Seq[B { type T = X }] forSome { type X }) = true

tells compiler that the function baz takes a Seq of the trait B's. 告诉编译器函数baz采用特征B的Seq。 The trait B happens to take a type member T but as far as compiler is concerned, it's totally fine for the function baz to take a Seq of the trait B's even if the types of the B's type member T are not the same. 特征B碰巧采用类型成员T,但就编译器而言,即使B类型成员T的类型不相同,函数baz也可以采用特征B的Seq。 If you want the function to baz to take a Seq of the trait B with the same type members, you need to tell the compiler as the following: 如果你想让函数baz采用具有相同类型成员的特征B的Seq,你需要告诉编译器如下:

scala> def baz[X](bs: Seq[B { type T = X }]) = true
baz: [X](bs: Seq[B{type T = X}])Boolean


scala> baz[Int](Seq(new B { type T = Int }, new B { type T = String }))
    <console>:10: error: type mismatch;
    found   : java.lang.Object with B
    required: B{type T = Int}
              baz[Int](Seq(new B { type T = Int }, new B { type T = String }))
                                               ^

scala> baz[Int](Seq(new B { type T = Int }, new B { type T = Int }))
res10: Boolean = true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM