Surprising behaviour of @enum conversion from Integer types

I have this code :

@enum EnumTest::UInt32 begin
	ONE = 1
	TWO = 2
end

@show EnumTest(2) # this works
convert(EnumTest, 2) # this fails

The output is

EnumTest(2) = TWO::EnumTest = 0x00000002
ERROR: LoadError: MethodError: Cannot `convert` an object of type Int64 to an object of type EnumTest
Closest candidates are:
  convert(::Type{T}, !Matched::T) where T at essentials.jl:154
  EnumTest(::Integer) at Enums.jl:134

I should have thought the second candidate would be matched. What’s happening?

Thanks.

1 Like