问题
Problem:
trait UpperBound[O]
trait High[F[O] <: UpperBound[O]]
def canEqual(that :Any) = that.isInstanceOf[High[_]]
def high(h :High[_]) = ???
Does not compile, because scalac sees the _ type instead of a type constructor it expects. How to fix it, ideally without writing a novel?
Original question (before edits in reply to Dmytro's answer) had:
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] }] = ???
Is there a shorter way of writing the above two methods by using some wildcard expression?
Simply using _ in High's type parameter position doesn't work as the kind doesn't match, and _[_] is not even a valid type expression.
回答1:
If you make existential quantization outside
Highthen it's justtype T = High[F] forSome { type F[O] <: UpperBound[O] } def canEqual(that: Any) = that.isInstanceOf[T] def high(h: T) = ???If you make existential quantization inside
Highthen sinceimplicitly[(n forSome { type n <: Upper}) =:= Upper] implicitly[(m[O1] forSome { type m[O] <: UpperBound[O]}) =:= UpperBound[O1]](and vice versa) it's just
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𝑄contains a clause type𝑡[tps]>:𝐿<:𝑈is equivalent to the type𝑇′ forSome { 𝑄 }where𝑇′results from𝑇by replacing every covariant occurrence of𝑡in𝑇by𝑈and by replacing every contravariant occurrence of𝑡in𝑇by𝐿.https://scala-lang.org/files/archive/spec/2.13/03-types.html#simplification-rules
来源:https://stackoverflow.com/questions/61041172/is-there-a-shorthand-for-type-variable-m-forsome-type-mo-upperboundo