Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docstring setindex #32738

Merged
merged 6 commits into from
Oct 2, 2019
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions base/tuple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,21 @@ getindex(t::Tuple, b::AbstractArray{Bool,1}) = length(b) == length(t) ? getindex
getindex(t::Tuple, c::Colon) = t

# returns new tuple; N.B.: becomes no-op if i is out-of-bounds

"""
setindex(c::Tuple, v, i::Integer)

Creates a new tuple similar to `x` with the value at index `i` set to `v`.
An out-of-bound `i` makes it a no-op.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is that intentional? Seems like odd behavior. I would assume it should boundscheck by default?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No idea, @vtjnash was the last to edit the line

Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was introduced in #20154 as a simple helper function. The behavior is very much intentional there, but perhaps we should see if we can add a @boundscheck block without sacrificing performance if we're going to move towards this being more documented.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it be considered as a breaking change then (replacing noop with BoundsError)?

Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really as it was neither exported nor documented. That's why if we document it, it'd be good to make sure the semantics are what we want.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


# Examples
```jldoctest
julia> Base.setindex((1, 2, 6), 2, 3) == (1, 2, 2)
true
```
"""
setindex(x::Tuple, v, i::Integer) = (@_inline_meta; _setindex(v, i, x...))

function _setindex(v, i::Integer, first, tail...)
@_inline_meta
return (ifelse(i == 1, v, first), _setindex(v, i - 1, tail...)...)
Expand Down