function foo(x::Tuple{Array{Float64,2}, String, Bool})
a, s, b = x
if a != []
println(a)
end
end
function main()
foo(([], "", false))
end
main()
ERROR: LoadError: MethodError: no method matching foo(::Tuple{Array{Any,1},String,Bool})
I’ll just type annotate the empty array…
function main()
foo(([]::Array{Float64,2}, "", false))
end
ERROR: LoadError: TypeError: in typeassert, expected Array{Float64,2}, got Array{Any,1}
I get this problem a lot…
Strangely i always want to solve it my annotating the type in the caller like I’ve done, and everytime i do that i remember that i can’t do that. it’s seems very natural, am i getting Julia mixed up with another language ?
I eventually solve it by removing the declaration, but I’m not going to do that any more. Some kind soul on the Julia forum is going to show me the error in my ways
The typessert does not work as you think it works. It will try to convert and throw an exception if it is wrong, and if used in a binding (not a literal) it will force that binding to only accept values of that type. It will not, however, serve to define which is the element type of the array literal. In such case, you declare the array as Type[...] for an array of type Type and contents ....
Yes, widening signatures like that is almost always the best answer. Not only will that work with [], it’ll also make your function just work for things like Diagonals, static arrays, sparse matrices, etc., etc.
not needed, but desirable. they this came up when I tried to pass an Array{Float64,1} instead of Array{Float64,2} and spent the next 5 minutes trying to figure out why i was getting an error message…
Yes, sometimes. I tend in a current project to the same because making the types explicit helps me to recognize what the function has to do somehow. The speaking name together with the parameter types does help me to be more productive.
function main()
foo((Array{Float64,2}[], "", false))
end
ERROR: LoadError: MethodError: no method matching foo(::Tuple{Array{Array{Float64,2},1},String,Bool})
Yes, I was wrong about the notation, the [[] []] is actually needed, but the correct form is typeof((Float64[[] []], "", false)) not the one presented by @oheil, I will edit the original.
julia> typeof((Float64[[][]], "", false))
ERROR: BoundsError: attempt to access 0-element Array{Any,1} at index []
Stacktrace:
[1] throw_boundserror(::Array{Any,1}, ::Tuple{}) at .\abstractarray.jl:537
[2] checkbounds at .\abstractarray.jl:502 [inlined]
[3] _getindex at .\abstractarray.jl:1002 [inlined]
[4] getindex(::Array{Any,1}) at .\abstractarray.jl:980
[5] top-level scope at REPL[1]:1
OH! if you look very carefully at my example you’ll see that i’m missing a space !!
so tricksy…
my bad, thank you for your solution
haha. in looking at your original reply , the line split right between the two ‘[ ]’ elements. lol. that’s why i didn’t realize there was a space there. Good to know that the space is significant.
Oh, yes, spacing is significant in Julia (while not so much like Python indentation). I recommend always copying an entire block of code, instead of copying until the end of the line, and then the rest, to avoid such problems.