`show` versus `parse` and arrays with 2 dimensions but only one column

The documentation for show states: “Write a text representation of a value x … and should be parseable Julia code when possible”. So I took that to mean that show and Main.eval(Meta.parse()) should (if poss) be inverses of one-another.

For arrays of Numbers, they generally are inverses. But not in the case of a two-dimensional array with a single column, for which I find that “showing then parsing” converts from two dimensions to one dimension".

What “array literal” will be parsed to a Matrix with one column?

julia> function myshow(x)
       io = IOBuffer()
       show(io,x)
       String(take!(io))
       end
myshow (generic function with 1 method)

julia> myeval(x) = Main.eval(Meta.parse(x))
myeval (generic function with 1 method)

#Round-tripping works for Vector
julia> x = collect(1:4)
4-element Vector{Int64}:
 1
 2
 3
 4

julia> myshow(x)
"[1, 2, 3, 4]"

julia> myeval(myshow(x)) == x
true

#Round-tripping works for 2-d array with 2 columns
julia> y = fill(1,(2,2))
2×2 Matrix{Int64}:
 1  1
 1  1

julia> myeval(myshow(y)) == y
true

#Round-tripping fails for 2-d array with 1 column
julia> z = fill(1,(3,1))
3×1 Matrix{Int64}:
 1
 1
 1

julia> myshow(z)
"[1; 1; 1]"

julia> myeval(myshow(z))
3-element Vector{Int64}:
 1
 1
 1

julia> myeval(myshow(z)) == z
false

To crosspost from slack:

The function myshow is called repr:

julia> a = [1; 2;;]
2×1 Matrix{Int64}:
 1
 2

julia> repr(a)
"[1; 2;;]"

julia> eval(Meta.parse(repr(a))) == a
true

Strangely enough that does not work for me:

julia> a = [1; 2;;]
2-element Vector{Int64}:
 1
 2

julia> versioninfo()
Julia Version 1.6.3
Commit ae8452a9e0 (2021-09-23 17:34 UTC)
Platform Info:
  OS: Windows (x86_64-w64-mingw32)
  CPU: Intel(R) Core(TM) i7-8650U CPU @ 1.90GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-11.0.1 (ORCJIT, skylake)
Environment:
  JULIA_NUM_THREADS = 8
  JULIA_EDITOR = code

Ah, that may be because I’m on a more recent branch - basically just a few days old version - of julia. You can try the 1.7 release candidate and check if it’s fixed there already (should be, they haven’t diverged much yet).

As a side note, the pattern you used in myshow of constructing an IOBuffer, writing to it and converting to string, is already implemented in the standard function sprint:

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

julia> sprint(show, x)
"[1, 2, 3, 4]"