Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What methods are generated for Scala case classes?

What methods are generated for Scala case classes?

I know that some methods are generated specifically for case classes:

  • equals
  • canEqual

What are the others?

Also, I see that I can call productArity() on any case class. How does this work? In other words, why the following code is valid?

case class CaseClass()

object CaseClass {
  val cc = new CaseClass()
  cc.productArity
}
like image 590
ilinum Avatar asked Nov 22 '25 10:11

ilinum


1 Answers

A good way what methods are generated for a specific class in Scala is to use the javap command.

Find the .class file that was compiled by scalac and then run the javap -private command on it from your respective command line tool. This will show you the constructors, fields, and all methods for a class.

You can do this for your case class to see what kinds of things are automagically supplied by Scala.

Case classes mixin the Product trait which provides the productArity method. For case classes the productArity method will return the count of the parameter list supplied in the class definition.

like image 138
nattyddubbs Avatar answered Nov 25 '25 06:11

nattyddubbs



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!