user2153969
user2153969

Reputation: 1

How can I construct a Dynamic Enum in scala

How can I construct a Dynamic EnumCreator in Scala?

class EnumCreater{
  def create[T <: Enum[_]](clazz :Class[T],input:String):T = Enum.valueOf(clazz,input)
}

sclac compiler report:

error: inferred type arguments [T] do not conform to method valueOf's type parameter bounds [T <: java.lang.Enum[T]]

I tried asInstanceOf to workaround this issue,It doesn't work. So what shall I do?

Upvotes: 0

Views: 381

Answers (1)

EECOLOR
EECOLOR

Reputation: 11244

In order to make it compile you need to replace the _ with T

def create[T <: Enum[T]](clazz: Class[T], input: String): T = 
  Enum.valueOf(clazz, input)

Upvotes: 1

Related Questions