Tur*_*rin 1 types scala existential-type higher-kinded-types
问题:
trait UpperBound[O]
trait High[F[O] <: UpperBound[O]]
def canEqual(that :Any) = that.isInstanceOf[High[_]]
def high(h :High[_]) = ???
Run Code Online (Sandbox Code Playgroud)
不编译,因为 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] }] = ???
Run Code Online (Sandbox Code Playgroud)
是否有使用通配符表达式编写上述两种方法的更短方法?简单地使用_inHigh的类型参数位置不起作用,因为类型不匹配,_[_]甚至不是有效的类型表达式。
如果你在外面进行存在量化,High那么它只是
type T = High[F] forSome { type F[O] <: UpperBound[O] }
def canEqual(that: Any) = that.isInstanceOf[T]
def high(h: T) = ???
Run Code Online (Sandbox Code Playgroud)如果你在里面进行存在量化,High那么因为
implicitly[(n forSome { type n <: Upper}) =:= Upper]
implicitly[(m[O1] forSome { type m[O] <: UpperBound[O]}) =:= UpperBound[O1]]
Run Code Online (Sandbox Code Playgroud)
(反之亦然)只是 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]) = ???
Run Code Online (Sandbox Code Playgroud)
一种存在类型
forSome { },其中包含一个子句类型[tps]>:<:等效于类型? forSome { },其中?从结果通过替换每协变发生在通过和通过替换每一个逆变发生在通过。
https://scala-lang.org/files/archive/spec/2.13/03-types.html#simplification-rules