Reputation: 2924
1 Why is it that when I define a variable and its type inside of a function everything's fine, but when it's a global variable I get error? Example:
n::Int8 = 3
ERROR: LoadError: UndefVarError: n not defined
2 Why is it that when I do some simple operations the variable type changes? Examples:
julia> function main()
n::Int8 = 5
c = collect(1:n)
println(c)
println(typeof(c))
end
main (generic function with 1 method)
julia> main()
[1,2,3,4,5]
Array{Int64,1}
julia> n = zero(Int8)
0
julia> typeof(ans)
Int8
julia> n += 5
5
julia> typeof(ans)
Int64
3 How to maintain the variable initial type?
Upvotes: 3
Views: 243
Reputation: 4366
Currently (Julia 0.4), questions 1 & 2 are answered by this statement in the Julia manual:
Currently, type declarations cannot be used in global scope, e.g. in the REPL, since Julia does not yet have constant-type globals.
As discussed in the comments, there are several options for #3.
Upvotes: 2