How to programmatically determine if the given class is a case class or a simple class?
相关问题
- Unusual use of the new keyword
- What uses more memory in c++? An 2 ints or 2 funct
- Get Runtime Type picked by implicit evidence
- How Does WebSphere Choose the Classloading Order i
- What's the point of nonfinal singleton objects
相关文章
- Gatling拓展插件开发,check(bodyString.saveAs("key"))怎么实现
- RDF libraries for Scala [closed]
- Why is my Dispatching on Actors scaled down in Akk
- How do you run cucumber with Scala 2.11 and sbt 0.
- GRPC: make high-throughput client in Java/Scala
- NameError: name 'self' is not defined, eve
- Setting up multiple test folders in a SBT project
- Testing request with CSRF Token in Play framework
Using new Scala reflection API:
Currently (2011), you can use reflection to find out if the class implements the interface
scala.Product
:This is just an approximation - you could go further and check if it has a
copy
method, if it implementsSerializable
, if it has a companion object with an appropriateapply
orunapply
method - in essence, check for all the things expected from a case class using reflection.The scala reflection package coming in one of the next releases should make case class detection easier and more precise.
EDIT:
You can now do it using the new Scala Reflection library -- see other answer.
If you mean: Can I determine whether a class is a case class or a non-case class programmatically, the answer is no, but you can do an approximation. Case classes are just a compiler hack, they tell the compiler to create certain methods etc. In the final bytecode, there is no difference between normal classes and case classes.
From How does a case class differ from a normal class?
So you can actually create a case class by just defining the correct methods & companion objects yourself.
For an idea as to how to find out if a class could be a case class, look at the answer from axel22.