How to programmatically determine if the given class is a case class or a simple class?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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
copymethod, if it implementsSerializable, if it has a companion object with an appropriateapplyorunapplymethod – 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.