From 7537c36399073b40d70eff52a7db292469a085fe Mon Sep 17 00:00:00 2001 From: Simon Byrne Date: Fri, 5 Jan 2018 15:27:25 -0800 Subject: [PATCH 1/2] allow use of begin blocks inside enum definitions --- base/Enums.jl | 12 ++++++++++++ test/enums.jl | 8 ++++++++ 2 files changed, 20 insertions(+) diff --git a/base/Enums.jl b/base/Enums.jl index 48c7233178359..b69f40ccf2618 100644 --- a/base/Enums.jl +++ b/base/Enums.jl @@ -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 + `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. @@ -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")) diff --git a/test/enums.jl b/test/enums.jl index 068aafff29e49..97f83810920bf 100644 --- a/test/enums.jl +++ b/test/enums.jl @@ -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 From b0d9dba3e96098dac0ff124de4b8d9a77be41634 Mon Sep 17 00:00:00 2001 From: Alex Arslan Date: Fri, 5 Jan 2018 16:00:06 -0800 Subject: [PATCH 2/2] Minor documentation adjustment, add NEWS --- NEWS.md | 4 ++++ base/Enums.jl | 10 ++++++---- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/NEWS.md b/NEWS.md index cb23c20919ccc..b512e2c1b363b 100644 --- a/NEWS.md +++ b/NEWS.md @@ -40,6 +40,9 @@ New language features * Field access via dot-syntax can now be overloaded by adding methods to `Base.getproperty` and `Base.setproperty!` ([#1974]). + * Values for `Enum`s can now be specified inside of a `begin` block when using the + `@enum` macro ([#25424]). + Language changes ---------------- @@ -1162,3 +1165,4 @@ Command-line option changes [#25184]: https://github.com/JuliaLang/julia/issues/25184 [#25231]: https://github.com/JuliaLang/julia/issues/25231 [#25365]: https://github.com/JuliaLang/julia/issues/25365 +[#25424]: https://github.com/JuliaLang/julia/issues/25424 diff --git a/base/Enums.jl b/base/Enums.jl index b69f40ccf2618..3e20f055fe175 100644 --- a/base/Enums.jl +++ b/base/Enums.jl @@ -48,10 +48,12 @@ julia> f(apple) Values can also be specified inside a `begin` block, e.g. - @enum EnumName begin - value1 - value2 - end +```julia +@enum EnumName begin + value1 + value2 +end +``` `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`