score:18
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 addsapply
andunapply
methods to its companion object.
score:0
I would kindly like to add one piece of information to the accepted answer that wasn't clear to me after reading it a couple of times; it's from the Scala Book (all credits to mlachkar, 0x54321 and alvinj):
A companion object in Scala is an object that’s declared in the same file as a class, and has the same name as the class. For instance, when the following code is saved in a file named
Pizza.scala
, the Pizza object is considered to be a companion object to the Pizza class:class Pizza { } object Pizza { }
Source: stackoverflow.com
Related Query
- Case class and companion object
- Scala case class and companion object can't override apply with diff signature
- Scala Generics with Case Class and Object Companion
- Scala: order of definition for companion object vs case class
- Companion class requires import of Companion object methods and nested objects?
- What's the difference between a class with a companion object and a class and object with the same name?
- How do I create an explicit companion object for a case class which behaves identically to the replaced compiler provided implicit companion object?
- Case class companion object generation error for compound type
- Compile error when using a companion object of a case class as a type parameter
- generic trait taking a class and it's companion object as a type parameter
- Mapped projection with <> to a case class with companion object in Slick
- An object extends its companion case class in Scala
- IntelliJ: Jump between companion object and class
- Companion object in Scala isn't associating itself with case class
- What's the difference between a companion object and a singleton class in Scala (Guice)
- factory object and case class
- Why does SonarQube find this issue (<static initializer for >() uses a Side Effect Constructor) with case and object class files?
- Implicit resolution for different orders of case class and companion
- Differences between case object T and case class T() when defining ADT?
- In ScalaPb, How to create a case object and case class extending from the same trait?
- Access companion object from case class (or vice-versa) using scala type macros
- How can a class and companion object see private vals in Scala?
- Retrieve fieldname and value from case class object in scala, at runtime
- Mapping case class with companion object
- Scala problems with slick 2, when case class and object name are same
- Scala ambiguous case class generated and companion apply method
- Generate companion object for case class with methods (field = method)
- Difference between instantiating something from the class and from the companion object
- Extends class with constructor and companion object
- scala use same logger into class and companion object
More Query from same tag
- Fast huffman decode
- Kmeans - group by
- The cost of nested methods
- Spray rejections is not converted to status code?
- What is the efficient way to create Spark DataFrame in Scala with array type columns from another DataFrame that does not have an array column?
- Scala local type inference underscore notation
- How to extract every n continuous elements in a list in scala?
- convert a byte array to string
- Build Jenkins pipeline using HttpRequest
- Parsing a String to Boolean or Int
- How to iterate over JSValue parsed from JSON String
- How to create list by deriving another list in scala
- Migrate Public/Private Keys from one programming language to other for Signing Payload and verifying them
- how to create and match schema in scala
- Spark job running out of heap memory on takeSample
- Missing parameter type error with Iterable Trait method in Scala.
- How can Scala actors return a value in response to a message?
- How to show messages only few members?
- How to check User input type(Data type) in scala?
- Making one Option[List[MyType]] from three different Option[List[MyType]]
- Read HBase in Scala - it.nerdammer
- Postgresql money (or numeric?) type and its corresponding type in Scala (Java)
- Join three tables in Scala Slick or flatten nested tuples
- Spark check if any words from array of dataframe is contained in another list?
- What 28 frames are elided when dividing by zero in the REPL?
- scala lambda function and local variables
- How to reference a typed class in ScalaDoc Documentation (version 2.10.6)
- spark.time() not working for dataframe query
- How to parameterize table name in Slick
- How to pass a dataframe as a function parameter in Spark