[英]Access member of underlying type of sequence (Scala)
package p {
trait A{ type B }
}
trait C {
val D: Seq[p.A]
def m(t: D. ...) = ???
}
I would like to refer to type B
through D
.我想通过D
引用类型B
。 How can I do this?我怎样才能做到这一点?
Note笔记
The code that I inherited was:我继承的代码是:
package p {
trait A{ type B }
}
trait C {
val D: p.A
def m(t: D.B) = ???
}
This makes sense in that context and certainly compiles.这在这种情况下是有意义的,并且肯定可以编译。
This code uses path-dependent types.此代码使用路径相关类型。
trait A { type B }
class A1 extends A { type B = String }
class A2 extends A { type B = Int }
In each of these types implementation uses different type B
.在这些类型中的每一个中,实现都使用不同的type B
。 So you cannot just refer to type B
directly, only through a path from the value.因此,您不能只通过值的路径直接引用类型B
trait Foo {
type Bar
def operationA(bar: Bar) = X
def operationB(bar: Bar) = Y
}
val foo1: Foo = ...
val foo2: Foo = ...
Here I cannot know of foo1.Bar =:= foo2.Bar
.在这里我不知道foo1.Bar =:= foo2.Bar
。 I don't know anything about it.我对此一无所知。 I only know that I could pass foo1.Bar
into foo1.operationA
or foo1.operationB
and pass foo2.Bar
into foo2.operationA
or foo2.operationB
.我只知道我可以将foo1.Bar
传递给foo1.operationA
或foo1.operationB
并将foo2.Bar
传递给foo2.operationA
或foo2.operationB
。
This is used correctly in the original code because you are fixing D to be a specific immutable value ( D
).这在原始代码中正确使用,因为您将 D 固定为特定的不可变值( D
)。 So the types working on this value are referring to it ( DB
).所以处理这个值的类型是指它( DB
)。
Seq
of D
cannot be used that way. D
的Seq
不能这样使用。 You would have to do something like你将不得不做类似的事情
package p {
trait A{ type B }
}
trait C {
val Ds: Seq[p.A]
def m(D: p.A)(t: D. ...) = ???
}
and then while working on Ds
passing each value of if into m
first, followed by a value derived from it (so that it would have this D. ...
type) next.然后在处理Ds
时,首先将 if 的每个值传递给m
,然后是从它派生的值(以便它具有这个D. ...
类型)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.