Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -816,6 +816,8 @@ iterate(A::Array, i=1) = (@_inline_meta; (i % UInt) - 1 < length(A) ? (@inbounds
Retrieve the value(s) stored at the given key or index within a collection. The syntax
`a[i,j,...]` is converted by the compiler to `getindex(a, i, j, ...)`.

See also [`get`](@ref), [`keys`](@ref), [`eachindex`](@ref).

# Examples
```jldoctest
julia> A = Dict("a" => 1, "b" => 2)
Expand Down
3 changes: 3 additions & 0 deletions base/dict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,9 @@ end
Return the value stored for the given key, or the given default value if no mapping for the
key is present.

!!! compat "Julia 1.7"
For tuples and numbers, this function requires at least Julia 1.7.

# Examples
```jldoctest
julia> d = Dict("a"=>1, "b"=>2);
Expand Down
3 changes: 3 additions & 0 deletions base/number.jl
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ function getindex(x::Number, I::Integer...)
@boundscheck all(isone, I) || throw(BoundsError())
x
end
get(x::Number, i::Integer, default) = i == 1 ? x : default
get(f::Callable, x::Number, i::Integer) = i == 1 ? x : f()

first(x::Number) = x
last(x::Number) = x
copy(x::Number) = x # some code treats numbers as collection-like
Expand Down
11 changes: 11 additions & 0 deletions test/numbers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2307,6 +2307,17 @@ end
@test_throws BoundsError getindex(x, 1, 0)
end
end
@testset "get(x::Number, ...)" begin
for x in [1.23, 7, ℯ, 4//5] #[FP, Int, Irrational, Rat]
@test get(x, 1, 99) == x
@test get(x, 2, 99) == 99
@test get(x, 0, pi) == pi
c = Ref(0)
@test get(() -> c[]+=1, x, 1) == x
@test get(() -> c[]+=1, x, 2) == 1
@test get(() -> c[]+=1, x, -1) == 2
end
end
@testset "copysign and flipsign" begin
# copysign(x::Real, y::Real) = ifelse(signbit(x)!=signbit(y), -x, x)
# flipsign(x::Real, y::Real) = ifelse(signbit(y), -x, x)
Expand Down