1

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?

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I've deleted my answer to avoid duplication, but you might want to mention that in 2.12 apply calls empty rather than the other way round.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.