Attempt to access Tuple{Float64, Bool} at index [0]

a = (1.0,false::Bool)

for i in a
    @show a[i]
end

and it returns

LoadError: BoundsError: attempt to access Tuple{Float64, Bool} at index [0]

however,

b = (1,2.0)
a[1]
length(a)
    
for i in 1:length(a)
    @show a[i]
end

for i in b
    @show a[i]
end

is it because I used different type in a tuple?

The syntax for elt in coll iterates on the elements of the collection. Therefore, in your example i takes in turn the values 1.0 (which, by chance, happens to be a valid index) and false (which is seen as 0 and is not a valid index).

TLDR: you can iterate directly on elements in the tuple:

julia> a = (1.0,false::Bool)
(1.0, false)

julia> for ai in a
           @show ai
       end
ai = 1.0
ai = false

Or you can use eachindex to iterate on valid indices:

julia> for i in eachindex(a)
           @show a[i]
       end
a[i] = 1.0
a[i] = false
3 Likes

Wait, what? Is 1.0 a valid index for tuples? It isn’t for vectors:

1.7.0> x = [1, 2];

1.7.0> x[1.0]
ERROR: ArgumentError: invalid index: 1.0 of type Float64

Why are they different?

My guess is that tuples were overlooked when non-integer indexing of vectors was removed.

$ ~/julia0.4/bin/julia
               _
   _       _ _(_)_     |  A fresh approach to technical computing
  (_)     | (_) (_)    |  Documentation: http://docs.julialang.org
   _ _   _| |_  __ _   |  Type "?help" for help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 0.4.7 (2016-09-18 16:17 UTC)
 _/ |\__'_|_|_|\__'_|  |  Official http://julialang.org/ release
|__/                   |  x86_64-pc-linux-gnu

julia> [1,2][1.0]
WARNING: Indexing with non-Integer Reals is deprecated.  It may be that your index arose from an integer division of the form i/j, in which case you should consider using i÷j or div(i,j) instead.
 in depwarn at deprecated.jl:73
 in to_index at deprecated.jl:447
 in getindex at array.jl:283
while loading no file, in expression starting on line 0
1

julia> (1,2)[1.0]
1
2 Likes

https://github.com/JuliaLang/julia/pull/43760

3 Likes