Not, I suspect, in the way you’re thinking. Values in Julia have types, variables are just labels attached to those values.
With that said, type declarations do have an effect in that they implicitly try to convert
the right-hand side to the specified type:
julia> function f()
x::Float64 = 1
@show typeof(x)
end
f (generic function with 1 method)
julia> f()
typeof(x) = Float64
What happened here is that the ::Float64
declaration caused Julia to insert a call to convert(Float64,...)
which does actually produce a value of type Float64
. You can see this in the @code_lowered
output:
julia> @code_lowered f()
CodeInfo(
1 ─ %1 = Base.convert(Main.Float64, 1)
│ x = Core.typeassert(%1, Main.Float64)
So the ::Float64
turned into a call to convert
and a call to typeassert
. That means that you’ll get the same result here by doing:
julia> function g()
x = convert(Float64, 1)
@show typeof(x)
end
g (generic function with 1 method)
julia> g()
typeof(x) = Float64
or more simply by doing Float64(1)
or just 1.0
.
So the annotation that you added to a variable here had an effect, but only because the convert
call that it inserted actually did change the type of the value being assigned to that variable.
This comes back to the same point here: How does a dictionary store structs? - #2 by rdeits – there is no distinction between “compile-time” and “run-time” types in Julia.