Skip to content

Commit b20c9ce

Browse files
committed
Make enum hash off by default for backward compat
1 parent 744ed17 commit b20c9ce

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

src/StructHelpers.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ const ENUM_BATTERIES_DEFAULTS = (
360360
string_conversion = false,
361361
symbol_conversion = false,
362362
selfconstructor = BATTERIES_DEFAULTS.selfconstructor,
363-
hash = BATTERIES_DEFAULTS.hash,
363+
hash = false,
364364
typesalt = BATTERIES_DEFAULTS.typesalt,
365365
)
366366

@@ -433,7 +433,7 @@ macro enumbatteries(T, kw...)
433433
""")
434434
end
435435
end
436-
nt = merge(ENUM_BATTERIES_DEFAULTS, nt)
436+
nt = merge(ENUM_BATTERIES_DEFAULTS, (; hash = haskey(nt, :typesalt)), nt)
437437
TT = Base.eval(__module__, T)::Type
438438
ret = quote end
439439

test/runtests.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,9 @@ end
134134
@enum Shape Circle = 7 Square = 8
135135
@enumbatteries Shape symbol_conversion = true typesalt = 0x0578044908fb9846
136136

137+
@enum Size Small Medium Large
138+
@enumbatteries Size hash = true
139+
137140
@testset "@enumbatteries" begin
138141
@test SH.has_batteries(Color)
139142
@test !SH.has_batteries(EnumNoBatteries)
@@ -169,9 +172,20 @@ end
169172
@test_throws Exception convert(String, Circle)
170173
@test_throws Exception Shape("Circle")
171174
@test_throws Exception convert(Shape, "Circle")
175+
end
172176

177+
@testset "@enumbatteries hash" begin
178+
# hash with typesalt
173179
@test hash(Circle) == hash(7, hash(0x0578044908fb9846))
174180
@test hash(Square) == hash(8, hash(0x0578044908fb9846))
181+
182+
# hash = true
183+
@test hash(Small) == hash(0, hash(Size))
184+
@test hash(Medium) == hash(1, hash(Size))
185+
@test hash(Large) == hash(2, hash(Size))
186+
187+
# no hash by default
188+
@test hash(Red) != hash(0, hash(Color))
175189
end
176190

177191
struct Bad end

0 commit comments

Comments
 (0)