Kvass
Kvass

Reputation: 8424

Scala - toList vs. result on ListBuffer?

The documentation for ListBuffers offer two methods that convert the ListBuffer into a List: result and toList.

result says it produces a collection from the added elements and that the contents are undefined afterward.

toList seems to instead make a constant-time lazy copy of the contents of the buffer (and presumably leaves the buffer intact).

If toList is constant time, when would we ever prefer result? And also am I understanding this correctly that toList will preserve the buffer's contents?

Upvotes: 2

Views: 1078

Answers (1)

The Archetypal Paul
The Archetypal Paul

Reputation: 41749

If we look at the source we see

def result: List[A] = toList

So (at least in the current version of Scala), there's no need to prefer one to the other.

Upvotes: 3

Related Questions