在 Scala 中是否有一个 shorthand for type variable 'm forSome { type m[O] <: UpperBound[O] }`?
Is there a shorthand for type variable 'm forSome { type m[O] <: UpperBound[O] }` in Scala?
问题:
trait UpperBound[O]
trait High[F[O] <: UpperBound[O]]
def canEqual(that :Any) = that.isInstanceOf[High[_]]
def high(h :High[_]) = ???
不编译,因为 scalac 看到 _
类型而不是它期望的类型构造函数。如何修复它,最好不写小说?
原始问题(在对 Dmytro 的回答进行编辑之前)有:
def canEqual(that :Any) = that.isInstanceOf[High[m forSome { type m[O] <: UpperBound[O] }]]
def high(h :High[m forSome { type m[O] <: UpperBound[O] }] = ???
有没有更短的方法可以使用通配符表达式来编写上述两个方法?
简单地在 High
的类型参数位置使用 _
是行不通的,因为种类不匹配,并且 _[_]
甚至不是一个有效的类型表达式。
如果你在 High
之外进行存在量化,那么它只是
type T = High[F] forSome { type F[O] <: UpperBound[O] }
def canEqual(that: Any) = that.isInstanceOf[T]
def high(h: T) = ???
如果在 High
内部进行存在量化,那么自
implicitly[(n forSome { type n <: Upper}) =:= Upper]
implicitly[(m[O1] forSome { type m[O] <: UpperBound[O]}) =:= UpperBound[O1]]
(反之亦然)只是 High[UpperBound]
implicitly[High[m forSome { type m[O] <: UpperBound[O] }] =:= High[UpperBound]]
def canEqual(that: Any) = that.isInstanceOf[High[UpperBound]]
def high(h: High[UpperBound]) = ???
An existential type forSome { }
where </code> contains a clause type <code>[tps]>:<:
is equivalent to the type ′ forSome { }
where ′
results from </code> by replacing every covariant occurrence of <code>
in </code> by <code>
and by replacing every contravariant occurrence of
in </code> by <code>
.
https://scala-lang.org/files/archive/spec/2.13/03-types.html#simplification-rules
问题:
trait UpperBound[O]
trait High[F[O] <: UpperBound[O]]
def canEqual(that :Any) = that.isInstanceOf[High[_]]
def high(h :High[_]) = ???
不编译,因为 scalac 看到 _
类型而不是它期望的类型构造函数。如何修复它,最好不写小说?
原始问题(在对 Dmytro 的回答进行编辑之前)有:
def canEqual(that :Any) = that.isInstanceOf[High[m forSome { type m[O] <: UpperBound[O] }]]
def high(h :High[m forSome { type m[O] <: UpperBound[O] }] = ???
有没有更短的方法可以使用通配符表达式来编写上述两个方法?
简单地在 High
的类型参数位置使用 _
是行不通的,因为种类不匹配,并且 _[_]
甚至不是一个有效的类型表达式。
如果你在
High
之外进行存在量化,那么它只是type T = High[F] forSome { type F[O] <: UpperBound[O] } def canEqual(that: Any) = that.isInstanceOf[T] def high(h: T) = ???
如果在
High
内部进行存在量化,那么自implicitly[(n forSome { type n <: Upper}) =:= Upper] implicitly[(m[O1] forSome { type m[O] <: UpperBound[O]}) =:= UpperBound[O1]]
(反之亦然)只是
High[UpperBound]
implicitly[High[m forSome { type m[O] <: UpperBound[O] }] =:= High[UpperBound]] def canEqual(that: Any) = that.isInstanceOf[High[UpperBound]] def high(h: High[UpperBound]) = ???
An existential type
forSome { }
where</code> contains a clause type <code>[tps]>:<:
is equivalent to the type′ forSome { }
where′
results from</code> by replacing every covariant occurrence of <code>
in</code> by <code>
and by replacing every contravariant occurrence ofin
</code> by <code>
.https://scala-lang.org/files/archive/spec/2.13/03-types.html#simplification-rules