Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Case class and companion object

Tags:

scala

I have problems understanding correctly the use of class and their companion object.

When defining a case class there is its companion object that comes with it, but what is the result of defining an object having the same name as the case class? Does it override the companion object? And how to access case class parameters?

For example in TestCaseClass.scala file I define the following :

case class TestCaseClass(att1: String, att2: Int, att4s: List[String])

object TestCaseClass {

  def iWantDoSomethingWithMyParams: String = {
    att1 + " " + att2
  }

  // Other functions
}

object AnotherTestCaseClass {

  def iWantDoSomethingWithTestCaseClassParams: String = {
  // How to access TestCaseClass.att1
    TestCaseClass.att1 + " " + TestCaseClass.att2
  }

  def iWantGetAllAttr4: List[String] = {
     // ???
  }
}
like image 214
jerome Avatar asked Apr 10 '16 10:04

jerome


People also ask

Can case class have companion object?

When you want to define some functionality related to a case class you have two possible ways to do this. The first one is to create functions directly in the case class. Note that in order to define a companion object for a class you have to set the same names for them and declare them in the same file.

What is a case class?

A case class has all of the functionality of a regular class, and more. When the compiler sees the case keyword in front of a class , it generates code for you, with the following benefits: Case class constructor parameters are public val fields by default, so accessor methods are generated for each parameter.

What is a companion object?

A companion object is an object that's declared in the same file as a class , and has the same name as the class. A companion object and its class can access each other's private members. A companion object's apply method lets you create new instances of a class without using the new keyword.

Is case a class singleton?

A case object, on the other hand, does not take args in the constructor, so there can only be one instance of it (a singleton like a regular scale object is). A case object is a singleton case class.


1 Answers

To some extent, giving an object the same name as a class (or trait) is just a matter of convention. But it also has a bit of special meaning.

The companion object is a singleton class just like any other object. If you want a method in the companion object to interact with an instance of the class, you have to pass it an instance of the class just like in any other situation. So, to fix your first example:

case class TestCaseClass(att1: String, att2: Int, att4s: List[String])

object TestCaseClass {    
  def iWantDoSomethingWithMyParams(x: TestCaseClass): String =
    x.att1 + " " + x.att2
}

The class and the object do not "override" or step on each other's toes in any way because classes and objects belong to different namespaces. Class names are used at the type level (and also in constructor calls), and object names are used at the term level.

There are a few relationships between a class and its companion:

  • It does affect how implicits are resolved - Any implicts defined in a class's companion object are automatically brought into scope.

  • private members of the class are visible to the object, and vice versa.

  • Case classes are a little bit different, because case class is actually a shorthand which, in addition to defining a class, also adds apply and unapply methods to its companion object.

like image 69
Chris Martin Avatar answered Nov 02 '22 23:11

Chris Martin