Eyal Roth
Eyal Roth

Reputation: 4019

Scala bound type parameter and reflection

Could the following Java code:

public <T extends Enum<T>> T foo(Class<T> clazz) {

  return clazz.getEnumConstants()[0];
}

public void bar(Class<?> clazz) {

    if (Enum.class.isAssignableFrom(clazz)) {
        System.out.println(foo(clazz.asSubclass(Enum.class)));
    } else if (String.class.isAssignableFrom(clazz)) {
        System.out.println("Meow");
    }
}

bar(MyEnum.class) // prints the first value of MyEnum
bar(String.class) // prints Meow

be translated to Scala:

bar[MyEnum]()
bar[String]()

Enum is just an example of a class that follows the T extends Wrapper[T] pattern, and foo could have simply returned the name of the class (or perform any other kind of logic which requires reflective data that is only available in my "Wrapper" class).

I tried to make it work in Scala with TypeTag but failed; I got all sort of compilation errors, such as this: inferred type arguments [?0] do not conform to method foo's type parameter bounds [E <: Enum[E]]

Upvotes: 2

Views: 393

Answers (2)

Giovanni Caporaletti
Giovanni Caporaletti

Reputation: 5546

You can try a typeclass approach, so that everything is resolved at compile-time, no reflection involved:

import scala.reflect.ClassTag

trait DoSomething[T] {
  def apply(): Unit
}

object DoSomething {
  implicit def enumDoSomething[E <: Enum[E]](implicit ct: ClassTag[E]) = new DoSomething[E] {
    def apply() = println(ct.runtimeClass.getEnumConstants()(0))
  }

  implicit object stringDoSomething extends DoSomething[String] {
    def apply() = println("Meow")
  }
}

object Test extends App {
  def foo[A](implicit doSomething: DoSomething[A]) = doSomething()

  foo[java.util.concurrent.TimeUnit]
  foo[String]
}

This way you don't have an if-else and you have a better separation of concerns.

if you want a "default" case, you can have a catch-all implicit.

import scala.reflect.ClassTag

trait DoSomething[T] {
  def apply(): Unit
}

object DoSomething {

  implicit def enumDoSomething[E <: Enum[E]](implicit ct: ClassTag[E]) = new DoSomething[E] {
    def apply() = println(ct.runtimeClass.getEnumConstants()(0))
  }

  implicit object stringDoSomething extends DoSomething[String] {
    def apply() = println("Meow")
  }

  implicit def catchAll[T] = new DoSomething[T] {
    def apply() = {
      println("test") // some default case
    }
  }
}

object Test extends App {
  def foo[A](implicit doSomething: DoSomething[A]) = doSomething()

  foo[java.util.concurrent.TimeUnit] // prints NANOSECONDS
  foo[String] // prints Meow
  foo[Long] // prints test
}

If you're interested in how scala finds implicits, take a look at this

Upvotes: 1

Alexey Romanov
Alexey Romanov

Reputation: 170723

There is quite likely a better way to do what you actually need, but:

import language.existentials
import reflect.ClassTag

def foo[T <: Enum[T]](implicit ct: ClassTag[T]) = ct

def bar[T](implicit ct: ClassTag[T]) = {
  val clazz = ct.runtimeClass

  if (classOf[Enum[_]].isAssignableFrom(clazz)) {
    println(foo(ct.asInstanceOf[ClassTag[A] forSome { type A <: Enum[A] }]))
  } else {
    println("not a enum")
  }
}

Upvotes: 3

Related Questions