Reputation: 295
I have just started learning Scala and I came across two syntax to initialize an empty ArrayBuffer
which are :
ArrayBuffer.empty[A]
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
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