Scala: Use case for Array.toArray?

Scala.Array includes the toArray function as an implicit import from ArrayOps .

Are there any use cases for Array.toArray or will it always return a copy of the object?


source to share

1 answer


inherits toArray

from GenTraversableOnce

(and a default implementation is provided in TraversableOnce


In case Array

it is meaningless, but the method exists for all other subclasses GenTraversableOnce

such as Map

, List

, Set

and many others.

Likewise, Map

inherits a meaningless method toMap

, List

a toList

, Set

a toSet

, etc.

In the specific case toArray

, the default implementation specified in the attribute isTraversableOnce

overridden by ArrayOps .

A call toArray

to n Array

will only return a new one if the runtime class for the destination type is different, otherwise it will simply cast Array

to the appropriate type and return the same instance.

So, generally speaking, calling toArray

on an instance is Array

useless, albeit not very expensive.



All Articles