From d623eb6805918ada63c7ef6a886737bda747ce98 Mon Sep 17 00:00:00 2001 From: Fredrik Ekre Date: Mon, 14 Mar 2022 13:59:16 +0100 Subject: [PATCH] Allow initialization of values from preview enum instances. --- src/EnumX.jl | 8 +++++++- test/runtests.jl | 8 ++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/EnumX.jl b/src/EnumX.jl index ccbb1b2..e167a2b 100644 --- a/src/EnumX.jl +++ b/src/EnumX.jl @@ -43,7 +43,13 @@ function enumx(_module_, name, args...) end sym = s elseif s isa Expr && s.head === :(=) && s.args[1] isa Symbol && length(s.args) == 2 - nx = Core.eval(_module_, s.args[2]) + if s.args[2] isa Symbol && + (i = findfirst(x -> x.first === s.args[2], name_value_map); i !== nothing) + @assert name_value_map[i].first === s.args[2] + nx = name_value_map[i].second + else + nx = Core.eval(_module_, s.args[2]) + end if !(nx isa Integer && typemin(baseT) <= nx <= typemax(baseT)) panic( "invalid value for Enum $(modname){$(baseT)}: " * diff --git a/test/runtests.jl b/test/runtests.jl index 041ff45..97fd280 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -4,6 +4,8 @@ using EnumX, Test const T16 = Int16 getInt64() = Int64 +const Elppa = -1 +const Ananab = -1 @testset "EnumX" begin @@ -192,4 +194,10 @@ let io = IOBuffer() @test str == "FruitDup.Apple = FruitDup.Banana = 0" end + +# Initialize with previous instance name +@enumx FruitPrev Elppa Banana=Elppa Orange=Ananab +@test FruitPrev.Elppa === FruitPrev.Banana === FruitPrev.Type(0) +@test FruitPrev.Orange === FruitPrev.Type(-1) + end # testset