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})
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})
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)
You can see here how Matrix
is defined in the Julia source code.
Note that the const
here is unnecessary. Type aliases like this will always be declared as constants.
PR to remove const
s from /base/arrays.jl ?
Way ahead of you : NFC: remove unnecessary const before type aliases by simeonschaub · Pull Request #40671 · JuliaLang/julia · GitHub
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
I could probably have been a little more precise. What I meant was type aliases declared with TypeVar
s on the left hand side, like Foo{T} = Bar{T}
. That doesn’t hold true for regular assignment of course.