Consider we have some abstract type
abstract type Point{T, N} end
What is the main difference between
Point3{T} = Point{T, 3}
and
const Point3{T} = Point{T, 3}
What is the const
modificator meaning related to the type declaration? I did not find exact explanation in the documentation.
mauro3
2
I think in this instance they are equivalent. consider
julia> Point3{T} = Point{T, 3}
Point{T,3} where T
julia> Point3 = 11
ERROR: invalid redefinition of constant Point3
Stacktrace:
[1] top-level scope at REPL[25]:1
thus Point3
is implicitly made a const
. However, if you don’t have type parameters, then you should use the const:
julia> Point3I = Point{Int, 3}
Point{Int64,3}
julia> Point3I = 11
11
as then the variable is not automatically marked as const
, and thus if you use it inside functions it will lead to type-unstable code.
To make the notation consistent, I would then also use const
in the first example, even though it is not needed.
2 Likes
Thank you for the explanation!