Iterating over an Enum

Hi,

quick question that I can’t see an answer to in the docs: how can I iterate over the members of an Enum?
So, I have:

@enum DistanceFunctionType chi_square d_and_s_type_a d_and_s_type_b 

and I want to write something like:

for method in DistanceFunctionType
   [do stuff..]
end

but that’s not allowed. Is there an idiomatic way to do this?

I’m new to Julia so apologies if this has in fact an obvious answer; I think I’ve searched all the usual places.

thanks,

Graham

3 Likes
help?> @enum
  @enum EnumName[::BaseType] value1[=x] value2[=y]

...

  To list all the instances of an enum use instances, e.g.

  julia> instances(Fruit)
  (apple::Fruit = 1, orange::Fruit = 2, kiwi::Fruit = 3)
julia> for m in instances(DistanceFunctionType)
           @show m
       end
m = chi_square::DistanceFunctionType = 0
m = d_and_s_type_a::DistanceFunctionType = 1
m = d_and_s_type_b::DistanceFunctionType = 2
6 Likes

Ah! Got it, thanks.