Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add broadcastable method for Enum #30670

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Multi-threading changes
Language changes
----------------

* `Enum` now behaves like a scalar when used in broadcasting ([#30670]).

Command-line option changes
---------------------------
Expand Down
1 change: 1 addition & 0 deletions base/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,7 @@ Base.RefValue{String}("hello")
```
"""
broadcastable(x::Union{Symbol,AbstractString,Function,UndefInitializer,Nothing,RoundingMode,Missing,Val}) = Ref(x)
Copy link
Contributor Author

@AzamatB AzamatB Jan 9, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we consolidate these definitions

broadcastable(x::Union{Symbol,AbstractString,Function,UndefInitializer,Nothing,RoundingMode,Missing,Val}) = Ref(x)
broadcastable(x::Enum) = Ref(x)
broadcastable(x::Ptr) = Ref(x)
broadcastable(r::Regex) = Ref(r)

as one into

broadcastable(x::Union{Symbol,AbstractString,Function,UndefInitializer,Nothing,RoundingMode,Missing,Val,Ptr,Regex,Enum}) = Ref(x)

?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Enum definition can't go here, since Enums.jl is loaded after this file. So you need to put Base.broadcastable(x::Enum) = Ref(x) into Enums.jl instead.

We could additionally consolidate this code somewhat as you say, but for simplicity you could leave it as-is in this PR.

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
Expand Down
3 changes: 3 additions & 0 deletions test/enums.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Fruit enum is already defined in this test file. So you can just do

@test (Vector{Fruit}(undef, 3) .= apple) == [apple, apple, apple]

(Note that there is no need to define the v variable, and the return value of .= is the lhs.)