So this is the relevant part of Statistics.jl:
_mean_promote(x::T, y::S) where {T,S} = convert(promote_type(T, S), y)
# ::Dims is there to force specializing on Colon (as it is a Function)
function _mean(f, A::AbstractArray, dims::Dims=:) where Dims
isempty(A) && return sum(f, A, dims=dims)/0
if dims === (:)
n = length(A)
else
n = mapreduce(i -> size(A, i), *, unique(dims); init=1)
end
x1 = f(first(A)) / 1
result = sum(x -> _mean_promote(x1, f(x)), A, dims=dims) #problem here
if dims === (:)
return result / n
else
return result ./= n
end
end
For some reason, when you call mean without the anonymous function the result variable correctly remains a UInt16, but when you add the arr->Matrix(arr) function, result is prematurely promoted to a Float64.