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

Should get work for tuples? #40809

Closed
mcabbott opened this issue May 13, 2021 · 6 comments
Closed

Should get work for tuples? #40809

mcabbott opened this issue May 13, 2021 · 6 comments
Labels
good first issue Indicates a good issue for first-time contributors to Julia kind:feature Indicates new feature / enhancement requests

Comments

@mcabbott
Copy link
Contributor

This seems surprising:

julia> get([1,2,3], 4, 5)
5

julia> get((1,2,3), 4, 5)
ERROR: MethodError: no method matching get(::Tuple{Int64, Int64, Int64}, ::Int64, ::Int64)

julia> get((a=1,b=2,c=3), :d, 5)
5

julia> get((a=1,b=2,c=3), 4, 5)
5
@simeonschaub simeonschaub added kind:feature Indicates new feature / enhancement requests good first issue Indicates a good issue for first-time contributors to Julia labels May 13, 2021
@xgupta
Copy link
Contributor

xgupta commented May 13, 2021

Would like to take it if it is a good first issue.

@xgupta
Copy link
Contributor

xgupta commented May 13, 2021

get function is define in base/dict.jl and testcase is in?

@simeonschaub
Copy link
Member

Typically files in base have a corresponding file in test with the same name, so test/dict.jl seems reasonable.

@mcabbott
Copy link
Contributor Author

I think dict.jl has the implementation for dictionaries, but not for other types, so would guess base/tuple.jl for this?

julia> @which get(Dict("a"=>1, "b"=>2), "a", 3)
get(h::Dict{K, V}, key, default) where {K, V} in Base at dict.jl:504

julia> @which get((a=1,b=2,c=3), :d, 5)
get(nt::NamedTuple, key::Union{Integer, Symbol}, default) in Base at namedtuple.jl:309

julia> @which get([1,2,3], 4, 5)
get(A::AbstractArray, i::Integer, default) in Base at abstractarray.jl:1469

The NamedTuple one is almost exactly what's needed.

Note that there's another method get(f, xs, i), which also seems to be missing for arrays:

julia> get(() -> println("runs"), (a=1,b=2,c=3), 3)
3

julia> get(() -> println("runs"), [1,2,3], 3)
ERROR: MethodError: no method matching get(::var"#17#18", ::Vector{Int64}, ::Int64)

@xgupta
Copy link
Contributor

xgupta commented May 13, 2021

Thanks, I understand the issue, I will send a PR soon.

Currently, I am reading the manual but now jump to Dict@Collections and Data Structures documentation to understand get and dict.

@xgupta
Copy link
Contributor

xgupta commented May 13, 2021

I think dict.jl has the implementation for dictionaries, but not for other types, so would guess base/tuple.jl for this?

Ah, now I understand the meaning of this, previous comment is not correct.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Indicates a good issue for first-time contributors to Julia kind:feature Indicates new feature / enhancement requests
Projects
None yet
Development

No branches or pull requests

3 participants