In Scala, suppose I have a case class like this:
case class Sample(myInt: Int, myString: String)
Is there a way for me to obtain a Seq[(String, Class[_])]
, or better yet, Seq[(String, Manifest)]
, describing the case class's parameters?
In Scala, suppose I have a case class like this:
case class Sample(myInt: Int, myString: String)
Is there a way for me to obtain a Seq[(String, Class[_])]
, or better yet, Seq[(String, Manifest)]
, describing the case class's parameters?
It's me again (two years later). Here's a different, different solution using Scala reflection. It is inspired by a blog post, which was itself inspired by a Stack Overflow exchange. The solution below is specialized to the original poster's question above.
In one compilation unit (a REPL
:paste
or a compiled JAR), includescala-reflect
as a dependency and compile the following (tested in Scala 2.11, might work in Scala 2.10):And in another compilation unit (the next line in the REPL or code compiled with the previous as a dependency), use it like this:
It seems like overkill, but I haven't been able to get it any shorter. Here's what it does:
caseClassFields
function creates an intermediateCaseClassFieldsExtractor
that implicitly comes into existence, reports its findings, and disappears.CaseClassFieldsExtractor
is a trait with a companion object that defines an anonymous concrete subclass of this trait, using a macro. It is the macro that can inspect your case class's fields because it has rich, compiler-level information about the case class.CaseClassFieldsExtractor
and its companion object must be declared in a previous compilation unit to the one that examines your case class so that the macro exists at the time you want to use it.WeakTypeTag
. This evaluates to a Scala structure with lots of pattern matching and no documentation that I could find.CaseClassFieldsExtractor
.caseClassFields
) without being called too early, when it's not yet defined.Any comments that could refine this solution or explain how exactly the "implicits" do what they do (or if they can be removed) are welcome.
Here's a different solution that uses plain-Java reflection.
To get a
Seq[(String, Class[_])]
, you can do this:I'm not sure about how to get
Manifests
.I'm answering my own question to provide a base solution, but I'm looking for alternatives and improvements, too.
One option, also compatible with Java and not restricted to case classes, is to use ParaNamer. In Scala, another option is to parse the
ScalaSig
bytes attached to generated classfiles. Both solutions won't work in the REPL.Here's my attempt at extracting the names of the fields from
ScalaSig
(which uses scalap and Scala 2.8.1):Disclaimer: I don't really understand the structure of ScalaSig and this should be considered as a heuristics. In particular, this code makes the following assumptions:
ClassSymbol
.MethodEntry
with name<init>
whose owner has id 0.It will fail (because of no
ScalaSig
) on nested case classes.This method also only returns
Class
instances and notManifest
s.Please feel free to suggest improvements!