KaustubhN
KaustubhN

Reputation: 295

Difference between initialization of empty ArrayBuffer using either "ArrayBuffer.empty[A]" or "ArrayBuffer[A]()" in Scala

I have just started learning Scala and I came across two syntax to initialize an empty ArrayBuffer which are :

  1. ArrayBuffer.empty[A]
  2. ArrayBuffer\[A]()

Is there any difference or preference in terms of usage of them as both are producing the same result when elements are added and played around with?

Upvotes: 1

Views: 296

Answers (1)

jwvh
jwvh

Reputation: 51271

They are the same, as witnessed by the source code:

def empty[A]: ArrayBuffer[A] = new ArrayBuffer[A]()

Not all types offer the <type>[Element]() form of initialization (see Option for example) but <type>.empty[Element] is pretty universal so it can be used even if it appears redundant and verbose.

Upvotes: 3

Related Questions