Difference between collect(x) and Array(x)?

Is there any difference in the behavior of collect(x) vs Array(x)?

  collect(element_type, collection)


  Return an Array with the given element type of all items in a collection or iterable. The result has the same shape and number of dimensions
  as collection.


julia> collect(1:5)
5-element Vector{Int64}:
 1
 2
 3
 4
 5

julia> Array(1:5)
5-element Vector{Int64}:
 1
 2
 3
 4
 5

Arrays are constructors and only work on things that are very Array-like, such as a range.

For example, Tuples are not array-like. They have their own purpose and behavior. So Array is not defined for them.


julia> Array((1, 2))
ERROR: MethodError: no method matching Array(::Tuple{Int64, Int64})
Closest candidates are:
  Array(::Union{LinearAlgebra.QR, LinearAlgebra.QRCompactWY}) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.6/LinearAlgebra/src/qr.jl:401
  Array(::Union{LinearAlgebra.Hermitian, LinearAlgebra.Symmetric}) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.6/LinearAlgebra/src/symmetric.jl:271
  Array(::LinearAlgebra.AbstractQ) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.6/LinearAlgebra/src/qr.jl:521
  ...
Stacktrace:
 [1] top-level scope
   @ REPL[2]:1

julia> collect((1, 2))
2-element Vector{Int64}:
 1
 2

In general, constructors, conversion, and “utility” methods like collect are distinct in Julia.

8 Likes