Literal representation of Array{T,2} when the array is nx1




How do a write a nx1 array literally such that it is an Array{T,2} ?

This all started when I tried to do the following,

julia> Array{UInt32,2}([1,2])
ERROR: MethodError: Cannot `convert` an object of type Array{Int64,1} to an object of type Array{UInt32,2}
This may have arisen from a call to the constructor Array{UInt32,2}(...),
since type constructors fall back to convert methods.

Solution should be easy, I just needed to write an Array{UInt64,2} literally. Another in the category of this should be easy to find, and yet…

julia> a=reshape([2,1],(2,1))
2×1 Array{Int64,2}:

julia> println(a)
[2; 1]

julia> [2;1]
2-element Array{Int64,1}:


I was expecting [2;1] to be Array{Int64,2}.

It seems like the written representation and the println representation should be consistent.



This should probably become a FAQ.


thank you.

an FAQ or maybe the search engine could have suggested that post ?

i’m sure the AI overlord in charge of searching will make searches more accurate in the future :wink:


The “proper” way is:

A=zeros(Int32, 2, 1)
A=Array{Int32, 2}(uninitialized, 2, 1)

and then fill it however you want. The only thing that is awkward are literal constructors, which are only useful on the repl anyway.

julia> i32ᵀ(x) = permutedims(Int32.(x)); # Julia 0.7
julia> # i32ᵀ(x) = permutedims(Int32.(x), (2,1)); # Julia 0.6

julia> i32ᵀ([1 2])
2×1 Array{Int32,2}:


or unit tests (for stuff like mapslices(f, matrix, 2)), and some other things.

I am undecided about the relevance of this issue: on the one hand it is nice to have literal syntax for things, on the other hand one clearly can’t have it for everything. But my impression is that this is an unfortunate corner case of the remnants of some Matlab-like syntax, and if we resolved that we could easily have it.


Fair enough; I stand corrected.