From fd7b172996938be8da71bde88449a6d0e3e8fe7d Mon Sep 17 00:00:00 2001 From: Azamat Date: Wed, 9 Jan 2019 21:52:12 -0500 Subject: [PATCH 1/2] Make Enum behave like a scalar in broadcasting --- NEWS.md | 1 + base/Enums.jl | 3 +++ test/enums.jl | 2 ++ 3 files changed, 6 insertions(+) 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 --------------------------- 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"))) """ diff --git a/test/enums.jl b/test/enums.jl index 71507487ac93f..e37cca18adadf 100644 --- a/test/enums.jl +++ b/test/enums.jl @@ -163,3 +163,5 @@ end haggis = 4 end @test Int(haggis) == 4 + +@test (Vector{Fruit}(undef, 3) .= apple) == [apple, apple, apple] From cfbce8f8a67815d4bee2f57a4bf606e86a6ea51e Mon Sep 17 00:00:00 2001 From: Azamat Date: Wed, 9 Jan 2019 23:08:28 -0500 Subject: [PATCH 2/2] Add Base. prefix --- base/Enums.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/base/Enums.jl b/base/Enums.jl index dfafd0e9c8912..d11a256497c1d 100644 --- a/base/Enums.jl +++ b/base/Enums.jl @@ -27,7 +27,7 @@ function membershiptest(expr, values) end # give Enum types scalar behavior in broadcasting -broadcastable(x::Enum) = Ref(x) +Base.broadcastable(x::Enum) = Ref(x) @noinline enum_argument_error(typename, x) = throw(ArgumentError(string("invalid value for Enum $(typename): $x")))