Repository: invenia/NamedDims.jl (julia)

Issue:
Broadcasting ambiguity
MWE:

```julia
using NamedDims
a = NamedDimsArray([1,1], :a)
b = (1,1)
g(x, y) = x + 1
g.(a, b)
```

```
MethodError: Base.Broadcast.BroadcastStyle(::NamedDims.NamedDimsStyle{Base.Broadcast.DefaultArrayStyle{1}}, ::Base.Broadcast.Style{Tuple}) is ambiguous.
```
<details> <summary> more ... </summary>

```
MethodError: Base.Broadcast.BroadcastStyle(::NamedDims.NamedDimsStyle{Base.Broadcast.DefaultArrayStyle{1}}, ::Base.Broadcast.Style{Tuple}) is ambiguous. Candidates:
  Base.Broadcast.BroadcastStyle(a::Base.Broadcast.AbstractArrayStyle, ::Base.Broadcast.Style{Tuple}) in Base.Broadcast at broadcast.jl:136
  Base.Broadcast.BroadcastStyle(::NamedDims.NamedDimsStyle{A}, b::B) where {A, B} in NamedDims at /Users/fabiangreimel/.julia/packages/NamedDims/12b8d/src/broadcasting.jl:28
  Base.Broadcast.BroadcastStyle(::Base.Broadcast.BroadcastStyle, ::Base.Broadcast.BroadcastStyle) in Base.Broadcast at broadcast.jl:130
Possible fix, define
  Base.Broadcast.BroadcastStyle(::NamedDims.NamedDimsStyle{A}, ::Base.Broadcast.Style{Tuple}) where A
result_style(::NamedDims.NamedDimsStyle{Base.Broadcast.DefaultArrayStyle{1}}, ::Base.Broadcast.Style{Tuple}) at broadcast.jl:445
combine_styles(::NamedDimsArray{(:a,),Int64,1,Array{Int64,1}}, ::Tuple{Int64,Int64}) at broadcast.jl:421
broadcasted(::Function, ::NamedDimsArray{(:a,),Int64,1,Array{Int64,1}}, ::Tuple{Int64,Int64}) at broadcast.jl:1263
top-level scope at aog.jl:185
include_string(::Function, ::Module, ::String, ::String) at loading.jl:1088
```

</details>

this bit me here https://github.com/JuliaPlots/AlgebraOfGraphics.jl/pull/80 and here https://github.com/JuliaPlots/Makie.jl/issues/709

Pull request:
Closes #140 

Figures it was worth at this point refacting out to an `@eval` loop

Patch:
diff --git a/Project.toml b/Project.toml
index e89152b..0610f2a 100644
--- a/Project.toml
+++ b/Project.toml
@@ -1,7 +1,7 @@
 name = "NamedDims"
 uuid = "356022a1-0364-5f58-8944-0da4b18d706f"
 authors = ["Invenia Technical Computing Corporation"]
-version = "0.2.27"
+version = "0.2.28"
 
 [deps]
 LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
diff --git a/src/broadcasting.jl b/src/broadcasting.jl
index cf4ec2b..c894f45 100644
--- a/src/broadcasting.jl
+++ b/src/broadcasting.jl
@@ -18,23 +18,29 @@ function NamedDimsStyle(a::BroadcastStyle, b::BroadcastStyle)
         return NamedDimsStyle(inner_style)
     end
 end
+
 function Base.BroadcastStyle(::Type{<:NamedDimsArray{L,T,N,A}}) where {L,T,N,A}
     inner_style = typeof(BroadcastStyle(A))
     return NamedDimsStyle{inner_style}()
 end
-
 function Base.BroadcastStyle(::NamedDimsStyle{A}, ::NamedDimsStyle{B}) where {A,B}
     return NamedDimsStyle(A(), B())
 end
-Base.BroadcastStyle(::NamedDimsStyle{A}, b::B) where {A,B} = NamedDimsStyle(A(), b)
-Base.BroadcastStyle(a::A, ::NamedDimsStyle{B}) where {A,B} = NamedDimsStyle(a, B())
-function Base.BroadcastStyle(::NamedDimsStyle{A}, b::DefaultArrayStyle) where {A}
-    return NamedDimsStyle(A(), b)
-end
-function Base.BroadcastStyle(a::AbstractArrayStyle{M}, ::NamedDimsStyle{B}) where {B,M}
-    return NamedDimsStyle(a, B())
+
+# Resolve ambiguities
+# for all these cases, we define that we win to be the outer style regardless of order
+for B in (
+    :BroadcastStyle, :DefaultArrayStyle, :AbstractArrayStyle, :(Broadcast.Style{Tuple}),
+)
+    @eval function Base.BroadcastStyle(::NamedDimsStyle{A}, b::$B) where A
+        return NamedDimsStyle(A(), b)
+    end
+    @eval function Base.BroadcastStyle(b::$B, ::NamedDimsStyle{A}) where A
+        return NamedDimsStyle(b, A())
+    end
 end
 
+
 """
     unwrap_broadcasted
 


Test patch:
diff --git a/test/broadcasting.jl b/test/broadcasting.jl
index 2e5e605..754065f 100644
--- a/test/broadcasting.jl
+++ b/test/broadcasting.jl
@@ -130,3 +130,13 @@ end
         @test typeof(parent(nda .- ta)) <: TrackedArray
     end
 end
+
+@testset "Broadcasting against Tuples" begin
+    # https://github.com/invenia/NamedDims.jl/issues/140
+    nda = NamedDimsArray(zeros(4), :foo)
+    @test (nda .+ (1,2,3,4)) isa NamedDimsArray{(:foo,)}
+    @test (nda .+ (1,2,3,4)) == [1, 2, 3, 4]
+
+    @test ((1,2,3,4) .+ nda) isa NamedDimsArray{(:foo,)}
+    @test ((1,2,3,4) .+ nda) == [1, 2, 3, 4]
+end