score:17
Let's call the class class SomeClass
(though it could also be e.g. a trait
).
Private members
Methods of the companion object (object SomeClass
) have access to private methods/data of instances of class SomeClass
.
If your companion object only uses the public interface of your class (e.g. just defines constants), there's no practical difference. But there are a number of cases where it's useful to let utility functions access private members. For example, object SomeClass
could define a factory method apply
that sets up private members of class SomeClass
, without having to expose setters in the public interface. In such cases, you must therefore define a companion object by putting the definition of object SomeClass
in the same compilation unit as class SomeClass
.
Another difference is that the compiler searches for implicits in companion objects of a type (and its supertypes). So if you are using implicit conversions you define in the code of class SomeClass
, you must define them in the companion object.
Comments
The combination of the two also explains the same-compilation-unit restriction.
scalac
can't compileobject SomeClass
until it knows what private members ofclass SomeClass
it calls.scalac
can't compileclass SomeClass
until it knows what implicits it calls. So the companion object must be compiled no later thanclass SomeClass
.
It follows they must be compiled at the same time. Further, the current compiler apparently compiles separate files separately (cf. the lack of support for splitting classes across multiple files), restricting it to the same compilation unit.
Source: stackoverflow.com
Related Query
- What's the difference between a class with a companion object and a class and object with the same name?
- What's the difference between a companion object and a singleton class in Scala (Guice)
- Difference between instantiating something from the class and from the companion object
- Scala what is the difference between defining a method in the class instead on the companion object
- What is the difference between object A extends B and class A extends B and later new B
- Difference between object and class in Scala
- What is the difference between Scala's case class and class?
- What is the difference between a class and a type in Scala (and Java)?
- Scala: what is the real difference between fields in a class and parameters in the constructor
- What is the difference between "class C extends A with B" and "class C extends B" when trait B extends trait A
- Difference between conversion with implicit function and implicit class in Scala
- Difference between a class snippet and an object snippet
- IntelliJ: Jump between companion object and class
- Difference between object with main() and extends App in scala
- What's the difference between abstract classes with zero parameters, and those with no parameters?
- What is the specific difference between a sealed trait and an abstract sealed class in scala?
- Whats the difference between RoundRobinPool and RoundRobinRouter
- What is the difference between parametric polymorphism with upper-bound and subtype polymorphism in these methods?
- Uses of the type class and difference between type class and type trait
- Extends class with constructor and companion object
- Scala case class and companion object can't override apply with diff signature
- What are the differences difference between `lazy` class variables and `lazy` local variables in a closure?
- What Is The Difference Between Functions Definitions And Class Definitions
- Scala Generics with Case Class and Object Companion
- Difference between case class with parentheses and without
- What are the differences between "object equality" created by the factory method of the companion object and case class?
- Is there a way to make companion object function private and import the companion object to the class and then access the function?
- What's the difference between object in scalajs scope and *same* object in js.global scope?
- Whats the difference between main and secondary methods in scala and how to execute mulitple functions in the scala class?
- whats the difference between src file and Target file
More Query from same tag
- Findig the 2nd last item in the list, please explain this solution
- Scala io.source class v object
- Issues in loading multiple csv in spark with scala
- Consider a trait as a subclass of another class
- Conditional routes/routing and Play 2.0
- Transform Dataframe of two columns into sorted list
- scalac for Call-by-Name use references
- Scala: Update class field value
- scala Compilation Error Implicit session
- ambigious implicits value - twirl form error
- Represnt List of list to string with a deliminator
- Using java formatter in scala
- Add custom compile time checks to Scala
- Maven project from SBT project convertor?
- Handling schema mismatches in Spark
- Scala Play 2: action composition and BodyParser
- sbt: cyclic dependence between modules?
- Scala - Overriding trait method using a sub context bound method
- Both XPath/getChildElements failed to get XML child in XOM
- Scala SBT throwing IncompatibleClassChangeError exception
- Scala Future onComplete callback not executing immediately
- How do I parse an xml document as a stream using Scala?
- Spark SQL Group By Consecutive Sequence of Integers
- running daemon in background in scala console
- Get field names list from case class
- Idiomatic sbt custom incremental work criteria
- Type safe aliases from sealed traits
- spark dataset from json with inner array
- How to write a One to Many Query with Scala Slick which returns something like this `(Model1, Option[Seq[Model2]])`
- Does extending a class in scala inherits auxilary constuctor also?