Using the new Scala final case class

In chapter 22 of Scala Programming, the class ::(cons) is defined as

final case class ::[T](hd: T, tl: List[T]) extends List[T] {
  //...
}

The method ::in the class Listis defined as follows:

def ::[U >: T](x: U): List[U] = new scala.::(x, this)

Why newis it necessary to create an instance ? Is that pure for meanings?final case class ::

+5
source share
2 answers

In class classes, you automatically get a companion object, the method applycalls the constructor, just as you can do this with a regular class:

class Foo(val value: Int) 
object Foo { def apply(value: Int) = new Foo(value) }

val x = new Foo(42)  //
val y = Foo(42)      // both work the same

new, . , apply, , , .

, new - .

+6

; new . List#:: :

def ::[U >: T](x: U): List[U] = scala.::(x, this)

(, :

type :: = collection.immutable.::
val  :: = collection.immutable.::

scala; -, new scala.::(x, this) , - scala.::(x, this).)

, , , . apply -, case ::, . , ? ( , , , apply, JVM .) , :

def ::[U >: T](x: U) = ::(x, this)

- (.. ) , , :: List , ::, , .

, .

+3

All Articles