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

allow use of begin blocks inside enum definitions #25424

Merged
merged 2 commits into from
Jan 6, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 12 additions & 0 deletions base/Enums.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ julia> f(apple)
"I'm a Fruit with value: 1"
```

Values can also be specified inside a `begin` block, e.g.

@enum EnumName begin
value1
value2
end
Copy link
Member

Choose a reason for hiding this comment

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

It would be nice to wrap this in triple backticks with a julia annotation so that it will be properly highlighted in the rendered docs.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Go ahead :)

Copy link
Member

Choose a reason for hiding this comment

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

Okay. I'll add a NEWS item too.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

thanks!


`BaseType`, which defaults to [`Int32`](@ref), must be a primitive subtype of `Integer`.
Member values can be converted between the enum type and `BaseType`. `read` and `write`
perform these conversions automatically.
Expand All @@ -69,7 +76,12 @@ macro enum(T, syms...)
lo = hi = 0
i = zero(basetype)
hasexpr = false

if length(syms) == 1 && syms[1] isa Expr && syms[1].head == :block
syms = syms[1].args
end
for s in syms
s isa LineNumberNode && continue
if isa(s, Symbol)
if i == typemin(basetype) && !isempty(vals)
throw(ArgumentError("overflow in value \"$s\" of Enum $typename"))
Expand Down
8 changes: 8 additions & 0 deletions test/enums.jl
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,11 @@ let b = IOBuffer()
seekstart(b)
@test deserialize(b) === apple
end

# test block form
@enum BritishFood begin
blackpudding = 1
scotchegg = 2
haggis = 4
end
@test Int(haggis) == 4