I am a little bit confused using companion objects in scala. When you want to provide multiple constructors, usually you declare a companion object and overload the apply
method. But what is the difference between this two ways of doing it?:
case class Node(....)
object Node {
def apply(...) = new Node(....) // 1 way
def apply(...) = Node(...) // second way
}
Almost all examples I've seen use the first form:
- When to use companion object factory versus the new keyword
- "new" keyword in Scala
- http://alvinalexander.com/scala/how-to-create-scala-object-instances-without-new-apply-case-class
But my code seems to work the same using both forms. Does using new
keyword only have sense when we have a normal class? (Not a case class)?