From 94b1be8610d273ea1d4df030b70d44c2f4bd8338 Mon Sep 17 00:00:00 2001 From: AzamatB Date: Wed, 9 Jan 2019 15:53:04 -0500 Subject: [PATCH 1/4] add method to broadcastable for Enum --- base/broadcast.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/base/broadcast.jl b/base/broadcast.jl index 194bbd489a515..024f1c77e8a77 100644 --- a/base/broadcast.jl +++ b/base/broadcast.jl @@ -606,6 +606,7 @@ Base.RefValue{String}("hello") ``` """ broadcastable(x::Union{Symbol,AbstractString,Function,UndefInitializer,Nothing,RoundingMode,Missing,Val}) = Ref(x) +broadcastable(x::Enum) = Ref(x) broadcastable(x::Ptr) = Ref(x) broadcastable(::Type{T}) where {T} = Ref{Type{T}}(T) broadcastable(x::Union{AbstractArray,Number,Ref,Tuple,Broadcasted}) = x From 2d88f3b535ab9931a64d409cd706dc8be5b8f2c4 Mon Sep 17 00:00:00 2001 From: AzamatB Date: Wed, 9 Jan 2019 16:57:11 -0500 Subject: [PATCH 2/4] Enums are scalars in broadcasting --- NEWS.md | 1 + 1 file changed, 1 insertion(+) diff --git a/NEWS.md b/NEWS.md index 685572a3fa480..cd0799ca681b8 100644 --- a/NEWS.md +++ b/NEWS.md @@ -16,6 +16,7 @@ Multi-threading changes Language changes ---------------- +* `Enum` now behaves like a scalar when used in broadcasting ([#30670]). Command-line option changes --------------------------- From 31d3b061687450d73af2474ff56cb61082f7c5fe Mon Sep 17 00:00:00 2001 From: AzamatB Date: Wed, 9 Jan 2019 17:27:44 -0500 Subject: [PATCH 3/4] tests for scalar behavior in broadcast --- test/enums.jl | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test/enums.jl b/test/enums.jl index 71507487ac93f..81b789b899030 100644 --- a/test/enums.jl +++ b/test/enums.jl @@ -163,3 +163,6 @@ end haggis = 4 end @test Int(haggis) == 4 + +# test scalar behavior in broadcast +@test (@enum Fruit apple; v = Vector{Fruit}(undef, 3); v .= apple; v) == [apple, apple, apple] From fabd74f4e178aabddeaf83e7d0757069dfea33d7 Mon Sep 17 00:00:00 2001 From: AzamatB Date: Wed, 9 Jan 2019 21:15:44 -0500 Subject: [PATCH 4/4] Update Enums.jl --- base/Enums.jl | 3 +++ 1 file changed, 3 insertions(+) diff --git a/base/Enums.jl b/base/Enums.jl index 87f99e7114789..dfafd0e9c8912 100644 --- a/base/Enums.jl +++ b/base/Enums.jl @@ -26,6 +26,9 @@ function membershiptest(expr, values) end end +# give Enum types scalar behavior in broadcasting +broadcastable(x::Enum) = Ref(x) + @noinline enum_argument_error(typename, x) = throw(ArgumentError(string("invalid value for Enum $(typename): $x"))) """