Printing an array without element type


#1

For example, when I print an array of complex numbers, the result shows the element type as “Complex{Float64}”:

julia> c = rand(Complex128,3); println("c = $c")
c = Complex{Float64}[0.77556+0.100567im, 0.482976+0.657349im, 0.411924+0.0728219im]

Is there an easy way to prevent this information from showing up? In other words, I would like it to be

c = [0.77556+0.100567im, 0.482976+0.657349im, 0.411924+0.0728219im]

Of course, I can achieve this by something like

julia> println("c = $(string(c)[17:end])")
c = [0.77556+0.100567im, 0.482976+0.657349im, 0.411924+0.0728219im]

or even

julia> println("c = $(string(c)[length(string(eltype(c)))+1:end])")
c = [0.77556+0.100567im, 0.482976+0.657349im, 0.411924+0.0728219im]

but I am wondering if there is a cleaner way.

I notice that the element type is not printed if the element type is Float64 or Int64:

julia> f = rand(3); println("f = $f")
f = [0.613025, 0.459371, 0.821628]

julia> i = rand(Int,3); println("i = $i")
i = [-8540970543845697954, 5703341344859102338, -5855558363139440179]

#2

You have to implement printing somehow (unfortunately, I don’t think Base.showarray is modular enough to do what you want, but maybe someone will provide a solution using it). This is another way:

function show_vector_sans_type(io, v::AbstractVector)
    print(io, "[")
    for (i, elt) in enumerate(v)
        i > 1 && print(io, ", ")
        print(io, elt)
    end
    print(io, "]")
end

show_vector_sans_type(v::AbstractVector) =
    show_vector_sans_type(IOContext(STDOUT; :compact => true), v)

show_vector_sans_type(x)

#3

@Tamas_Papp that is basically the type of function I am using in my Dendriform.jl package to print arrays.
I think if you have a specific need for printing things in a package, it makes sense to just define your own.

Another complaint I had previously is the spaces that have been inserted into the syntax, as I explained here:

My solution was to modify the Base.show_delim_array function to get rid of the spaces. (not showarray)