简体   繁体   中英

Is there a shorthand for type variable 'm forSome { type m[O] <: UpperBound[O] }` in Scala?

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.

  • If you make existential quantization outside High then it's just

    type 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 High then since

    implicitly[(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 { } wherecontains a clause type [tps]>:<: is equivalent to the type ′ forSome { } where results fromby replacing every covariant occurrence ofinbyand by replacing every contravariant occurrence ofinby.

    https://scala-lang.org/files/archive/spec/2.13/03-types.html#simplification-rules

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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