How is Matrix defined

I noticed that Matrix prints out that it is an alias. How would one define an alias for a custom type?

julia> Matrix{Int}
Matrix{Int64} (alias for Array{Int64, 2})
1 Like

Here’s a simple example:

julia> struct MyType{T, N}
       end

julia> const MyAlias{T} = MyType{T, 2}
MyAlias{T} where T (alias for MyType{T, 2} where T)
8 Likes

You can see here how Matrix is defined in the Julia source code.

2 Likes

Note that the const here is unnecessary. Type aliases like this will always be declared as constants.

1 Like

PR to remove consts from /base/arrays.jl :wink: ?

1 Like

Way ahead of you :wink: : NFC: remove unnecessary const before type aliases by simeonschaub · Pull Request #40671 · JuliaLang/julia · GitHub

3 Likes

In which Julia version were these const declarations necessary?

That’s just not true (on 1.6.1 at least). There is an observable difference between using const and not, and it’s what you’d expect:

julia> T = Int64
Int64

julia> T = Float64
Float64

vs

julia> const T = Int64
Int64

julia> T = Float64
ERROR: invalid redefinition of constant T
Stacktrace:
 [1] top-level scope
   @ REPL[2]:1
3 Likes

I could probably have been a little more precise. What I meant was type aliases declared with TypeVars on the left hand side, like Foo{T} = Bar{T}. That doesn’t hold true for regular assignment of course.

5 Likes