I read the manual about types carefully, but I can’t figure out how to do this.
Let’s two types of data are possible: Vector{Int8}
and Vector{Vector{Int8}}
eg [1, 0, 1]
and [[1, 1], [0]]
Following the recommendations, I wrote two implementations:
some_calc(vec::Vector{Int8})
and
some_calc(vec::Vector{Vector{Int8}})
They both work efficiently and recognise types.
But there are actions that have to be done anyway,
so there is a main function that should accept both types:
main_calc(vec::???)
…
some_calc(vec)
…
end
I tried to write Vector
in place of ???, or just vec
without type description -
nothing works. After studying the manual, I thought the right idea was
Union{Vector{Int8}, Vector{Vector{Int8}}}
But surprisingly
typeof([[1,2], [1,3]]) == Union{Vector{Int64}, Vector{Vector{Int64}}} → false
typeof([1,2,3]) == Union{Vector{Int64}, Vector{Vector{Int64}}} → false
I’m tracking the conversion Int64 → Int8
(it’s needed for performance, so there is no problem with it)
But the last results confused me!
PS. Why do I care so much about types? (Maybe it helps new users)
I do complex computations like fractal research.
And found that Julia is great for this, and with strict typing and right memory allocation much faster than Python etc. So Julia rocks!