Scala中存在的类型

Scala中存在的类型,scala,types,enums,existential-type,Scala,Types,Enums,Existential Type,关于存在类型有点混乱 这对我很有用: def valueOf(c: Class[_], name: String) { type C = Class[T] forSome {type T <: Enum[T]} Enum.valueOf(c.asInstanceOf[C], name) } def-valueOf(c:Class[;],name:String){ C型=类[T]对于某些{T型请考虑以下两个表达式之间的差异: Enum.valueOf(x.asInstanceOf

关于存在类型有点混乱

这对我很有用:

def valueOf(c: Class[_], name: String) {
  type C = Class[T] forSome {type T <: Enum[T]}
  Enum.valueOf(c.asInstanceOf[C], name)
} 
def-valueOf(c:Class[;],name:String){

C型=类[T]对于某些{T型请考虑以下两个表达式之间的差异:

Enum.valueOf(x.asInstanceOf[Class[X] forSome { type X <: Enum[X] }], name)
现在
foo(x)
将编译,但是
foo(y)
不会编译,就像在代码中一样。但是稍微更改
Bar

trait Foo[A]
trait Bar[+A]

def foo[A <: Bar[A]](f: Foo[A]) = f

def x: Foo[X] forSome { type X <: Bar[X] } = ???
def y: Foo[Y forSome { type Y <: Bar[Y] }] = ???
trait Foo[A]
特征条[+A]

def foo[A我不明白为什么对某些{type X}来说
X这么多年后,我意识到我的问题是多么愚蠢:)
inferred type arguments [T] do not conform to method valueOf's type parameter bounds [T <: Enum[T]]
         Enum.valueOf(c.asInstanceOf[Class[T]], name)
              ^
Enum.valueOf(x.asInstanceOf[Class[X] forSome { type X <: Enum[X] }], name)
Enum.valueOf(x.asInstanceOf[Class[X forSome { type X <: Enum[X] }]], name)
trait Foo[A]
trait Bar[A]

def foo[A <: Bar[A]](f: Foo[A]) = f

def x: Foo[X] forSome { type X <: Bar[X] } = ???
def y: Foo[Y forSome { type Y <: Bar[Y] }] = ???
trait Foo[A]
trait Bar[+A]

def foo[A <: Bar[A]](f: Foo[A]) = f

def x: Foo[X] forSome { type X <: Bar[X] } = ???
def y: Foo[Y forSome { type Y <: Bar[Y] }] = ???
scala> trait Foo[A <: Foo[A]]
defined trait Foo

scala> class MyFoo extends Foo[MyFoo]
defined class MyFoo

scala> val myFoo = new MyFoo
myFoo: MyFoo = MyFoo@3ee536d

scala> myFoo: (X forSome { type X <: Foo[X] })
res0: X forSome { type X <: Foo[X] } = MyFoo@3ee536d

scala> myFoo: Foo[MyFoo]
res1: Foo[MyFoo] = MyFoo@3ee536d
myFoo: Foo[X forSome { type X <: Foo[X] }]