Units, errors and type display

Better type-printing tools are on the way in 0.7 via showarg: julep: `Base.summary` and ShowItLikeYouBuildIt · Issue #18909 · JuliaLang/julia · GitHub
It doesn’t look like it’s been added to the online docs just yet, but it’s in the REPL help:

showarg(io::IO, x, toplevel)

Show x as if it were an argument to a function. This function is used by
summary to display type information in terms of sequences of function calls
on objects. toplevel is true if this is the direct call from summary and
false for nested (recursive) calls.

The fallback definition is to print x as ":::(typeof(x))", representing
argument x in terms of its type. (The double-colon is omitted if
toplevel=true.) However, you can specialize this function for specific types
to customize printing.

Example
≡≡≡≡≡≡≡≡≡

A SubArray created as view(a, :, 3, 2:5), where a is a 3-dimensional Float64
array, has type

SubArray{Float64,2,Array{Float64,3},Tuple{Colon,Int64,UnitRange{Int64}},false}

The default show printing would display this full type. However, the summary
for SubArrays actually prints as

2×4 view(::Array{Float64,3}, :, 3, 2:5) with eltype Float64

because of a definition similar to

  function Base.showarg(io::IO, v::SubArray, toplevel)
      print(io, "view(")
      showarg(io, parent(v), false)
      print(io, ", ", join(v.indices, ", "))
      print(io, ')')
      toplevel && print(io, " with eltype ", eltype(v))
  end

Note that we’re calling showarg recursively for the parent array type,
indicating that any recursed calls are not at the top level. Printing the
parent as ::Array{Float64,3} is the fallback (non-toplevel) behavior,
because no specialized method for Array has been defined.

1 Like