Any way to access the type of a Scala Option declaration at runtime using reflection?

Posted by Graham Lea on Stack Overflow See other posts from Stack Overflow or by Graham Lea
Published on 2010-04-18T21:50:17Z Indexed on 2010/04/18 21:53 UTC
Read the original article Hit count: 239

Filed under:
|
|
|
|

So, I have a Scala class that looks like this:

class TestClass {
  var value: Option[Int] = None
}

and I'm tackling a problem where I have a String value and I want to coerce it into that Option[Int] at runtime using reflection. To do so, I need to know that the field is an Option and that the type parameter of the Option is Int.

What are my options for figuring out that the type of 'value' is Option[Int] at runtime (i.e. using reflection)?

I have seen similar problems solved by annotating the field, e.g. @OptionType(Int.class). I'd prefer a solution that didn't require annotations on the reflection target if possible.

© Stack Overflow or respective owner

Related posts about scala

Related posts about reflection