Simplify signature of printed arrays

When one prints a simple array of floats, one gets:

julia> println(ones(3))
[1.0, 1.0, 1.0]

However, if I print an arrays of unitful quantities, I get:

julia> println(ones(3)u"nm")
Quantity{Float64, ๐‹, Unitful.FreeUnits{(nm,), ๐‹, nothing}}[1.0 nm, 1.0 nm, 1.0 nm]

Is there a way (shouldnโ€™t it?) print only [1.0 nm, 1.0 nm, 1.0 nm] without doing type-piracy?

Being clear: I donโ€™t want to change my implementation of the show methods of my types to adapt specifically to Unitful types. Unitful is not a dependency, just a nice thing to have. The thing is that some printouts, like this:

julia> box = Box(ones(3),0.1)
Box{OrthorhombicCell, 3, Float64, Float64, 9}
  unit cell matrix: [1.0 0.0 0.0; 0.0 1.0 0.0; 0.0 0.0 1.0]
  cutoff: 0.1
  number of computing cells on each dimension: [12, 12, 12]
  computing cell sizes: [0.1, 0.1, 0.1] (lcell: 1)
  Total number of cells: 1728

become way too ugly:

julia> box = Box(ones(3)u"nm",0.1u"nm")
Box{OrthorhombicCell, 3, Quantity{Float64, ๐‹, Unitful.FreeUnits{(nm,), ๐‹, nothing}}, Quantity{Float64, ๐‹^2, Unitful.FreeUnits{(nm^2,), ๐‹^2, nothing}}, 9}
  unit cell matrix: Quantity{Float64, ๐‹, Unitful.FreeUnits{(nm,), ๐‹, nothing}}[1.0 nm 0.0 nm 0.0 nm; 0.0 nm 1.0 nm 0.0 nm; 0.0 nm 0.0 nm 1.0 nm]
  cutoff: 0.1 nm
  number of computing cells on each dimension: [12, 12, 12]
  computing cell sizes: Quantity{Float64, ๐‹, Unitful.FreeUnits{(nm,), ๐‹, nothing}}[0.1 nm, 0.1 nm, 0.1 nm] (lcell: 1)
  Total number of cells: 1728

Any suggestion?

May be too basic for what you need, but just in case:

using Unitful
v = ones(3)u"nm"
println(ustrip(v), unit(v[1]))
[1.0, 1.0, 1.0]nm

There is

function show(io::IO, x::Unit{N,D}) where {N,D}
    show(io, FreeUnits{(x,), D, nothing}())
end

but unfortunately I didnโ€™t find a specialization for FreeUnits. So maybe an oversight?

1 Like

This seems simple and closer to your original request:

using Unitful
v = ones(3)u"nm"
println('[', join(v,", "), ']')
[1.0 nm, 1.0 nm, 1.0 nm]
1 Like

Another way using tuples:

using Unitful
v = ones(3)u"nm"
println(tuple(v...))
(1.0 nm, 1.0 nm, 1.0 nm)
1 Like

Nice, this one is quite generic, may be a good alternative. I will test it. Thanks @rafael.guerra, something like you suggested above did the trick.

1 Like