Variables indeed can have declared types without assigned values. Access without a value errors, and so does assignment of values that can’t be converted to the declared type. Note that the local
/global
keywords are necessary because x::Int
already means type assertion of the instance assigned to a variable.
julia> module TypedDemo
global x::Int
function foo(z)
local y::Int
y = z
end
end
Main.TypedDemo
julia> TypedDemo.x
ERROR: UndefVarError: `x` not defined in `Main.TypedDemo`
Suggestion: add an appropriate import or assignment. This global was declared but not assigned.
Stacktrace:
...
julia> TypedDemo.x = "one"
ERROR: MethodError: Cannot `convert` an object of type String to an object of type Int64
...
julia> TypedDemo.foo("one")
ERROR: MethodError: Cannot `convert` an object of type String to an object of type Int64
...
That alone doesn’t come close to the things discussed here, but maybe it can be useful.