-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Add keytype
+valtype
method for AbstractArray and AbstractVector
#27749
Conversation
The function `keytype(a)` returns `eltype(keys(a))` for associative structures; it should do the same for `Abstract{Array, Vector}`.
Wouldn't literally this definition be a better generic fallback and cover these cases? |
Agree that I think one stumbling block for arrays in the past is that they have at least two kinds of keys (linear and Cartesian) plus whatever we call it when you can |
Here's what made me not do that: And we'd need to implement it case-by-case for julia> d = Dict(1=>2)
Dict{Int64,Int64} with 1 entry:
1 => 2
julia> keytype(d), keytype(typeof(d))
(Int64, Int64)
julia> a = [1 2]
1×2 Array{Int64,2}:
1 2
julia> keytype(a), keytype(typeof(a))
ERROR: MethodError: no method matching keytype(::Type{Array{Int64,2}})
Closest candidates are:
keytype(::AbstractArray{T,1} where T) at abstractarray.jl:100
keytype(::AbstractArray) at abstractarray.jl:99
keytype(::Type{#s56} where #s56<:AbstractDict{K,V}) where {K, V} at abstractdict.jl:237
...
Stacktrace:
[1] top-level scope at none:0
julia> keytype(a)
CartesianIndex{2} Thoughts? |
Now also for the type, not just for objects.
I feel the fallback can be allowed to have allocations, whether or not the compler can elide them. If we have specific performance problems with
This is more convincing. |
Merge back master branch to validate clean merge and to kick off an up-to-date CI build.
Arrays are associative structures in the sense that keys(...) and values(...) both work on them. When considering them as such, their valtype should be equal to eltype. (A future version of Julia could even do `const valtype = eltype`?)
Reviving this pull request as I recently found myself reaching for this. Merged back master (still a clean merge) and added |
keytype
+valtype
method for AbstractArray and AbstractVector
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's make the type-based methods the "canonical" endpoints so a custom type would only need to override that one (instead of both).
As suggested by @mbauman in code review. Co-Authored-By: tkluck <tkluck@infty.nl>
All feedback is now incorporated, including the needs-* labels. The CI failures seem to be about dependency artifacts and not related to this PR. |
Oooh let's get this in 1.2: https://discourse.julialang.org/t/julia-1-2-feature-freeze-april-4th/22478 @mbauman or @andyferris , do either of you have commit rights? Or should we bring in someone else? |
It appears that this no longer needs NEWS or tests but still needs docs with compatibility annotation indicating that it was added in version 1.2. If @mbauman approves, we can merge. It would be great to get the docs with this PR but if time is an issue if you promise to add docs after the feature freeze... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @tkluck! This looks great to me now. Let's merge once tests pass.
@StefanKarpinski good catch, apologies. (FWIW, I (mis)understood that that label was related to the Compat.jl package, and that it would need the specific commit that introduced this feature -- i.e. that it wasn't actionable until this was merged.) Just pushed those docs with a compatibility note. |
Looks great! I made a suggestion to change the |
this makes the distinction between key and value clearer. Thanks to @StefanKarpinski for suggesting this. Co-Authored-By: tkluck <tkluck@infty.nl>
Travis uncovers failing
on 32-bits architecture. I'll check. |
This was a little red herring in the discussion in JuliaLang#27749: the `length` of a range may depend on the type of its parameters, but as it turns out, `eltype(keys(...))` does not. That's arguably a bug/inconsistency, but it's outside of the scope of this pull request to fix that.
buildbot is happy now, including the new test cases introduced by this pull request. Appveyor and travis complain about unrelated things and are broken on master too. I say, good to go :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good to go as soon as CI passes. Please squash when merging!
The function
keytype(a)
returnseltype(keys(a))
for associative structures; it should do the same forAbstractArray
andAbstractVector
. Looks like this was overlooked(?) in #25577.#25104 is a related pull request, but renaming is less urgent than adding expected behaviour.