Skip to content

extrema with an explicit dims argument is not inferred #39281

Closed
@yurivish

Description

@yurivish
julia> using Test

julia> Test.@inferred extrema(rand(2), dims=1)
ERROR: return type Array{Tuple{Float64,Float64},1} does not match inferred return type Array

This was brought this up on Slack and @YingboMa promptly suggested a diff:

diff --git a/base/multidimensional.jl b/base/multidimensional.jl
index 0fcd70b2d2..8d906a3ac0 100644
--- a/base/multidimensional.jl
+++ b/base/multidimensional.jl
@@ -1695,12 +1695,12 @@ of `A`.
 """
 extrema(f, A::AbstractArray; dims=:) = _extrema_dims(f, A, dims)

-_extrema_dims(f, A::AbstractArray, ::Colon) = _extrema_itr(f, A)
+_extrema_dims(f::F, A::AbstractArray, ::Colon) where F = _extrema_itr(f, A)

-function _extrema_dims(f, A::AbstractArray, dims)
-    sz = [size(A)...]
+function _extrema_dims(f::F, A::AbstractArray, dims) where F
+    sz = size(A)
     for d in dims
-        sz[d] = 1
+        sz = setindex(sz, 1, d)
     end
     T = promote_op(f, eltype(A))
     B = Array{Tuple{T,T}}(undef, sz...)

The lack of inference was due to lack of specialization on f and because sz was degrading the fixed-length size tuple to a variable-length vector.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions