JuliaArrays / StaticArrays.jl

Statically sized arrays for Julia
Other
774 stars 148 forks source link

Should Scalar(1) .+ 1 return 2 or Scalar(2)? #775

Open MasonProtter opened 4 years ago

MasonProtter commented 4 years ago

If was brought up here: https://github.com/JuliaLang/julia/pull/35591 that one nice property of Ref is that

julia> Ref(1) .+ 1
2

whereas

julia> Scalar(1) .+ 1
Scalar{Int64}((2,))

I'm not sure how I feel about this difference yet, but I figured I should open an issue to discuss it.

MasonProtter commented 4 years ago

More evidence perhaps pointing towards the idea that the output should be 2:

julia> A = Array{Int, 0}(undef)
0-dimensional Array{Int64,0}:
0

julia> A[] = 1
1

julia> A .+ 1
2
MasonProtter commented 4 years ago

The simplest way to get this behaviour for Scalar is to do

Base.BroadcastStyle(::Type{<:Scalar}) = Base.Broadcast.DefaultArrayStyle{0}()

but perhaps someone who understands broadcast internals better than me could just tweak StaticArrayStyle{0} to do the 'right' thing.

tkf commented 4 years ago

just tweak StaticArrayStyle{0} to do the 'right' thing

Sounds like the best approach to me.

c42f commented 4 years ago

I started rewriting StaticArrays broadcast for #744... just got bogged down and haven't had a chance to finish it, it's very fiddly! I suppose this could be considered at the same time.