Skip to content

Commit bb47a48

Browse files
committed
Broadcasting: resolve priority when combining dynamic & static axes
1 parent 561005d commit bb47a48

File tree

3 files changed

+16
-0
lines changed

3 files changed

+16
-0
lines changed

src/SOneTo.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,6 @@ end
5858
Base.@pure function Base.checkindex(::Type{Bool}, ::SOneTo{n1}, ::SOneTo{n2}) where {n1, n2}
5959
return n1::Int >= n2::Int
6060
end
61+
62+
Base.promote_rule(a::Type{Base.OneTo{T}}, ::Type{SOneTo{n}}) where {T,n} =
63+
Base.OneTo{promote_type(T, Int)}

src/broadcast.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
import Base.Broadcast:
66
BroadcastStyle, AbstractArrayStyle, Broadcasted, DefaultArrayStyle, materialize!
7+
import Base.Broadcast: _bcs1 # for SOneTo axis information
8+
using Base.Broadcast: _bcsm
79
# Add a new BroadcastStyle for StaticArrays, derived from AbstractArrayStyle
810
# A constructor that changes the style parameter N (array dimension) is also required
911
struct StaticArrayStyle{N} <: AbstractArrayStyle{N} end
@@ -37,6 +39,10 @@ end
3739
_broadcast!(f, destsize, dest, argsizes, as...)
3840
end
3941

42+
# Resolving priority between dynamic and static axes
43+
_bcs1(a::SOneTo, b::SOneTo) = _bcsm(b, a) ? b : (_bcsm(a, b) ? a : throw(DimensionMismatch("arrays could not be broadcast to a common size")))
44+
_bcs1(a::SOneTo, b::Base.OneTo) = _bcs1(Base.OneTo(a), b)
45+
_bcs1(a::Base.OneTo, b::SOneTo) = _bcs1(a, Base.OneTo(b))
4046

4147
###################################################
4248
## Internal broadcast machinery for StaticArrays ##

test/core.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,4 +183,11 @@
183183
@test !StaticArrays.sizematch(Size(2, 2), a)
184184
@test !StaticArrays.sizematch(Size(3, StaticArrays.Dynamic()), a)
185185
end
186+
187+
@testset "SOneTo" begin
188+
a = Base.OneTo(3)
189+
b = StaticArrays.SOneTo{2}()
190+
@test @inferred(promote(a, b)) === (a, Base.OneTo(2))
191+
@test @inferred(promote(b, a)) === (Base.OneTo(2), a)
192+
end
186193
end

0 commit comments

Comments
 (0)