# Where is "Vector", "Matrix" printed in \`typeof(::AbstractArray)\`

**URL:** <https://discourse.julialang.org/t/where-is-vector-matrix-printed-in-typeof-abstractarray/74299>\
**Category:** New to Julia\
**Tags:** question, type\
**Created:** [January 9, 2022, 6:53pm UTC](https://discourse.julialang.org/t/where-is-vector-matrix-printed-in-typeof-abstractarray/74299 "2022-01-09T18:53:23Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![tfiers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tfiers/32/32427_2.png) [@tfiers](https://discourse.julialang.org/u/tfiers)\
**Post date:** [January 9, 2022, 6:53pm UTC](https://discourse.julialang.org/t/where-is-vector-matrix-printed-in-typeof-abstractarray/74299/1 "2022-01-09T18:53:23Z")

</div>

I am looking for the code from Base that prints `Vector` or `Matrix` for a Vector or Matrix, and `Array` otherwise. I tried to track it down, and this seems to happen in `typeof(::AbstractArray`); but I can’t inspect that function, neither through `which` nor the debugger:

```julia
julia> [1]
1-element Vector{Int64}:
 1

julia> typeof([1])
Vector{Int64} (alias for Array{Int64, 1})

julia> @which typeof([1])
ERROR: ArgumentError: argument is not a generic function
Stacktrace:
 [1] which(f::Any, t::Any)
   @ Base .\reflection.jl:1320

```

How can I find this code?

(The reason I’m looking for it, is that I’m writing a custom `showarg(io::IO, x::AbstractArray{<:Unitful.Quantity{T,D,U}}, toplevel)`. Calling `nameof(typeof(x))` yields `"Array"`, even for vectors and matrices. I currently do

```julia
    print(io, x isa Vector ? "Vector" :
              x isa Matrix ? "Matrix" :
              nameof(typeof(x)))

```

but that seems non-ideal (i.e. for Vector subtypes)).

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [January 9, 2022, 6:55pm UTC](https://discourse.julialang.org/t/where-is-vector-matrix-printed-in-typeof-abstractarray/74299/2 "2022-01-09T18:55:43Z")

</div>

> [@tfiers](#):
>
> and this seems to happen in `typeof(::AbstractArray` )

`typeof()` merely returns you a type, you can skip one step:

```julia
julia> Vector{Int64}
Vector{Int64} (alias for Array{Int64, 1})

```

the line printed out you’re seeing comes from

```julia
julia> Vector{Int64};

julia> display(Vector{Int64});
Vector{Int64} (alias for Array{Int64, 1})

```

(you know this is where it comes from because `;` has suppressed the normal REPL display)

and I think the easiest way is:

```julia
julia> a = [1];

julia> b = [1 2];

julia> c = [1 2;;;3 4];

julia> string(typeof(a))
"Vector{Int64}"

julia> string(typeof(b))
"Matrix{Int64}"

julia> string(typeof(c))
"Array{Int64, 3}"

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [January 9, 2022, 7:26pm UTC](https://discourse.julialang.org/t/where-is-vector-matrix-printed-in-typeof-abstractarray/74299/3 "2022-01-09T19:26:51Z")

</div>

> [@jling](#):
>
> `julia> display(Vector{Int64});`

`display` ultimately calls `show(stdout, MIME("text/plain"), Vector{Int64})` as explained [here](https://docs.julialang.org/en/v1/manual/types/#man-custom-pretty-printing). You can find the definition at:

```julia
julia> @which show(stdout, MIME("text/plain"), Vector{Int64})
show(io::IO, ::MIME{Symbol("text/plain")}, x::Type) in Base at show.jl:855

```

[here in `show.jl`](https://github.com/JuliaLang/julia/blob/7499513e69f4b1af27000665908289e20b50e69b/base/show.jl#L853-L877).

> [@jling](#):
>
> and I think the easiest way is:
> 
> ```julia
> julia> string(typeof(a))
> "Vector{Int64}"
> 
> ```

In general `string(x)` calls `print(x)`, which typically calls `show(io, x)` (though there are some exceptions like strings that print differently). If you want the three-arg `show(io, "text/plain", x)` output as a string you can call `repr("text/plain", x)` or, most generally, `sprint(show, "text/plain", x)`.

But it sounds like @tfiers wants the output _without_ the type parameters but _with_ the alias?

---

<div class="post-metadata">

**Author:** ![tfiers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tfiers/32/32427_2.png) [@tfiers](https://discourse.julialang.org/u/tfiers)\
**Post date:** [January 9, 2022, 7:37pm UTC](https://discourse.julialang.org/t/where-is-vector-matrix-printed-in-typeof-abstractarray/74299/4 "2022-01-09T19:37:37Z")

</div>

That is exact @stevengj.  
We can split it off the string of course, but that’s not the cleanest.

---

<div class="post-metadata">

**Author:** ![eliascarv](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eliascarv/32/22152_2.png) [@eliascarv](https://discourse.julialang.org/u/eliascarv)\
**Post date:** [January 9, 2022, 7:41pm UTC](https://discourse.julialang.org/t/where-is-vector-matrix-printed-in-typeof-abstractarray/74299/5 "2022-01-09T19:41:01Z")

</div>

`Vector{T}` is just a alias for `Array{T, 1}`, the same way that `Matrix{T}` is a alias for `Array{T, 2}`.

So if you want to make a custom print for `AbstractVector` and `AbstractMatrix` subtypes you can do it like this:

```julia
function showarg(io::IO, x::AbstractArray{<:Unitful.Quantity{T,D,U}, 1}, toplevel) 
    # code for AbstractVector
end

function showarg(io::IO, x::AbstractArray{<:Unitful.Quantity{T,D,U}, 2}, toplevel) 
    # code for AbstractMatrix
end

```

Or;

```julia
function showarg(io::IO, x::AbstractVector{<:Unitful.Quantity{T,D,U}}, toplevel) 
    # code
end

function showarg(io::IO, x::AbstractMatrix{<:Unitful.Quantity{T,D,U}}, toplevel) 
    # code
end

```

---

<div class="post-metadata">

**Author:** ![tfiers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tfiers/32/32427_2.png) [@tfiers](https://discourse.julialang.org/u/tfiers)\
**Post date:** [January 9, 2022, 9:38pm UTC](https://discourse.julialang.org/t/where-is-vector-matrix-printed-in-typeof-abstractarray/74299/6 "2022-01-09T21:38:05Z")

</div>

Thanks for the helpfulness y’all. What a great community.

This is indeed what I’m after:

> [@stevengj](#):
>
> it sounds like @tfiers wants the output _without_ the type parameters but _with_ the alias

The code to print this clearly exists _somewhere_ in the julia codebase, I just don’t know where.  
I guess I’d like to see the source code of `typeof(::AbstractArray)`.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [January 9, 2022, 10:37pm UTC](https://discourse.julialang.org/t/where-is-vector-matrix-printed-in-typeof-abstractarray/74299/7 "2022-01-09T22:37:31Z")

</div>

> [@tfiers](#):
>
> The code to print this clearly exists _somewhere_ in the julia codebase, I just don’t know where.

I gave you the link above, to the `show` method, and you should look there to find the lower-level code for output of types. In general, how _anything_ in Julia is displayed or printed almost always boils down to `show`.

---

<div class="post-metadata">

**Author:** ![tfiers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tfiers/32/32427_2.png) [@tfiers](https://discourse.julialang.org/u/tfiers)\
**Post date:** [January 9, 2022, 10:37pm UTC](https://discourse.julialang.org/t/where-is-vector-matrix-printed-in-typeof-abstractarray/74299/8 "2022-01-09T22:37:57Z")

</div>

Yes, thank you. (I hadn’t actually clicked that link because I thought I knew the function it pointed to; but turned out it was a different `show` in Base 😅).

Exploring that function led me to the solution:

```julia
julia> Base.make_typealias(Vector{Int})[1].name
:Vector

```
