Skip to content

Commit

Permalink
Make isassigned work with tuples
Browse files Browse the repository at this point in the history
This commit adds a method `isassigned(v::Tuple, i::Integer)` which makes
that function work on `Tuple`s, the same way it does on `Vector`s.
(Since a `Tuple` cannot be created without being assigned, all that
is left is to check that the index is within bounds.)

This makes it easier to write generic code that works on both
`Vector`s and `Tuple`s.
  • Loading branch information
perrutquist authored and oscardssmith committed Jan 10, 2025
1 parent c1db3a4 commit a26c418
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
5 changes: 5 additions & 0 deletions base/tuple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,11 @@ revargs(x, r...) = (revargs(r...)..., x)

reverse(t::Tuple) = revargs(t...)

function isassigned(v::Tuple, i::Integer)
@boundscheck 1 <= i <= length(v) || return false
true
end

## specialized reduction ##

prod(x::Tuple{}) = 1
Expand Down
8 changes: 8 additions & 0 deletions test/tuple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -850,3 +850,11 @@ end
end
end
end

@testset "isassigned" begin
t = (1, 2, 3)
@test isassigned(t, 0) === false
@test isassigned(t, 1) === true
@test isassigned(t, 3) === true
@test isassigned(t, 4) === false
end

0 comments on commit a26c418

Please sign in to comment.