When a new value is needed in scala

Possible duplicate:
"new" keyword in Scala

Ive noticed that creating specific instances in Scala can be left without using new . When is a developer required to use new ? And why do some objects let you skip it?

thanks

The list ("this", "is", "a", "list") creates a list of these four lines; no new required

The map ("foo" → 45, "bar" → 76) creates a line map for Int; there are no new required and undeniable helper classes.

Taken from here ..

+4
source share
1 answer

In general, the scala collection classes define factory methods in their companion object using the apply method. List("this","is","a","list") and Map("foo" -> 45, "bar" ->76) are syntactic sugar for calling these applicable methods. Using this convention is pretty idiomatic scala.

In addition, if you define a case class C(i: Int) , it also defines a factory C.apply(i: Int) method, which can be called C(i) . So nothing new is needed.

In addition, new is required to create objects.

+10
source

Source: https://habr.com/ru/post/1402156/


All Articles