-
-
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
Array{T}(::T)
should return a zero-dimensional array
#54935
Comments
A more conservative option would be to define the constructor just for zero-dimensional arrays, something like: function Array{T,0}(x::T) where {T}
fill(x)::Array{T,0}
end IMO it'd make sense to try that in one PR, and then, after that PR is merged, maybe try with: function Array{T}(x::T) where {T}
Array{T,0}(x)
end |
Thanks, Just to be clear I don’t plan on adding this myself, just wanted to point it out as a sharp edge |
I'm not sure that this would eliminate sharp edges, though it might hide them slightly better in some cases. I'm sure you don't literally intend the title as written in full generality, as it would be a breaking change if What would counts as a scalar for this purpose? I think I'm not sure whether this can all fit nicely under existing functions. It may be easier to introduce a new function with a more specific purpose to accomplish your goal. It seems that you might be proposing something vague but tailored to a narrow use (like |
I don't feel strongly about this – I tried out zero-dimensional arrays in my code but ran into some unexpected behavior like this so stopped using them. But I thought I'd point this out for future generations in case there's a smart way to fix it.
Not sure. In some ways I feel like zero-dimensional arrays should just be eliminated altogether in Julia 2.0 in favor of just using scalars, 1-element vectors, or I suppose one motivation for them is so that you can store scalars on the GPU? (like
I agree I think julia> Array{Any}(zeros(2))
2-element Vector{Any}:
0.0
0.0
julia> Array{T}(x::T) where {T} = fill(x)
julia> Array{Any}(zeros(2))
2-element Vector{Any}:
0.0
0.0 |
Example: julia> x = MtlArray(fill(1f0))
0-dimensional MtlArray{Float32, 0, Private}:
1.0
julia> x .+ x
2.0f0 So it moves off the GPU. Maybe we shouldn't implement |
Isn't this effectively a duplicate (or, rather a downstream effect) of #28866? It seems like making |
yep, this totally seems to be the case. I'll close this. |
Because of the fact that broadcasted zero-dimensional arrays automatically convert to scalars (#30825) –
it would be nice to have
Array{T}
as a constructor for zero-dimensional arrays. For example, if I write out:This works for most cases:
but fails for zero-dimensional arrays:
I think that
Array{T}(::T)
returningfill(T)
seems to be a reasonable choice.Right now if you try to write a generic array converter, you either have something like:
meaning zero-dimensional arrays get turned into scalars. So one option is to use something like
for a converter, which works for 0-dimensional arrays:
but instead, fails on scalars, such as the result of
x .+ x
which means it's a bit of a sharp edge to write generic operations for any
AbstractArray{T,N}
unless imposing the restrictionN>0
.The text was updated successfully, but these errors were encountered: