# \#enum

**URL:** https://discourse.julialang.org/tag/enum/1101.md

[Latest](https://discourse.julialang.org/latest.md) · [Categories](https://discourse.julialang.org/categories.md) · [Tags](https://discourse.julialang.org/tags.md)

---

## [\[ANN\] InstanceDispatch.jl: Dispatch-on-value with anyting that defines a \`Base.instances\` method](https://discourse.julialang.org/t/ann-instancedispatch-jl-dispatch-on-value-with-anyting-that-defines-a-base-instances-method/130838)

<div class="topic-metadata">

**Author:** [@klafyvel](https://discourse.julialang.org/u/klafyvel)\
**Replies:** 8\
**Last updated:** [August 24, 2026, 12:56pm UTC](https://discourse.julialang.org/t/ann-instancedispatch-jl-dispatch-on-value-with-anyting-that-defines-a-base-instances-method/130838 "2026-08-24T12:56:28Z")

</div>

Hi there, I just started the registration process for InstanceDispatch.jl, a single-macro package that writes for you a method that dispatches on methods with value-type from a type that defines a Base.instances method …

---

## [Export enum](https://discourse.julialang.org/t/export-enum/5396)

<div class="topic-metadata">

**Author:** [@babaq](https://discourse.julialang.org/u/babaq)\
**Replies:** 5\
**Last updated:** [July 23, 2025, 2:06pm UTC](https://discourse.julialang.org/t/export-enum/5396 "2025-07-23T14:06:35Z")

</div>

Hi I am defining an enum such as @enum myenum enumname1 enumname2 in a module using Julia 0.6. it seems I have to explicitly export both enum type name(myenum) and enum elements(enumname1,enumname2). I know this way is …

---

## [Enums to string & string to enum: a simple guide to use enums & strings](https://discourse.julialang.org/t/enums-to-string-string-to-enum-a-simple-guide-to-use-enums-strings/126044)

<div class="topic-metadata">

**Author:** [@0nyr](https://discourse.julialang.org/u/0nyr)\
**Replies:** 0\
**Last updated:** [February 18, 2025, 8:57pm UTC](https://discourse.julialang.org/t/enums-to-string-string-to-enum-a-simple-guide-to-use-enums-strings/126044 "2025-02-18T20:57:50Z")

</div>

Practical guide to Julia Enums ↔ Strings Well, I do this post for the me of the past that spent way to much time on understanding Enums and how to deal with Symbols, Strings and Enums. Coming from Rust, dealing with Juli…

---

## [\[ANN\] EnumSets.jl](https://discourse.julialang.org/t/ann-enumsets-jl/123671)

<div class="topic-metadata">

**Author:** [@jw3126](https://discourse.julialang.org/u/jw3126)\
**Replies:** 6\
**Last updated:** [December 11, 2024, 3:57pm UTC](https://discourse.julialang.org/t/ann-enumsets-jl/123671 "2024-12-11T15:57:52Z")

</div>

I am pleased to announce EnumSets.jl. From the readme: EnumSets This packages allows to create a very fast immutable type that represents a set of enum values. julia\> using EnumSets julia\> @enum Lang Python Julia …

---

## [Is there a way to statically ensure an if-block covers all enum cases?](https://discourse.julialang.org/t/is-there-a-way-to-statically-ensure-an-if-block-covers-all-enum-cases/122426)

<div class="topic-metadata">

**Author:** [@NHDaly](https://discourse.julialang.org/u/NHDaly)\
**Replies:** 8\
**Last updated:** [November 9, 2024, 8:00pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-statically-ensure-an-if-block-covers-all-enum-cases/122426 "2024-11-09T20:00:56Z")

</div>

Consider: @enum Level LOW MEDIUM HIGH function do\_stuff(level::Level, args...) if level == HIGH println("High level") elseif level == MEDIUM println("Medium level") elseif level == LOW …

---

## [Best way to ensure only certain values are allowed for a particular arg?](https://discourse.julialang.org/t/best-way-to-ensure-only-certain-values-are-allowed-for-a-particular-arg/120952)

<div class="topic-metadata">

**Author:** [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Replies:** 4\
**Last updated:** [October 5, 2024, 10:17am UTC](https://discourse.julialang.org/t/best-way-to-ensure-only-certain-values-are-allowed-for-a-particular-arg/120952 "2024-10-05T10:17:53Z")

</div>

In trying to write a clone of numpy API in Julia, I see the numpy.array has an argument which accepts only 4 values but is other a string in Python. The 4 possibles values are C , F, A, K. So I want to make sure that ar…

---

## [Using \`Enum\`s as indices](https://discourse.julialang.org/t/using-enum-s-as-indices/115718)

<div class="topic-metadata">

**Author:** [@sgaure](https://discourse.julialang.org/u/sgaure)\
**Replies:** 3\
**Last updated:** [June 16, 2024, 7:02pm UTC](https://discourse.julialang.org/t/using-enum-s-as-indices/115718 "2024-06-16T19:02:50Z")

</div>

I’m using enum’s for some purposes. In particular for indexing arrays. It works fine, except for some types of broadcasting: a = \[zeros(10) for \_ in 1:4\]; b = \[zeros(10) for \_ in 1:4\]; @enum Count one=1 two three four …

---

## [Adding members to an existing Enum](https://discourse.julialang.org/t/adding-members-to-an-existing-enum/108962)

<div class="topic-metadata">

**Author:** [@egvila](https://discourse.julialang.org/u/egvila)\
**Replies:** 5\
**Last updated:** [January 19, 2024, 7:33pm UTC](https://discourse.julialang.org/t/adding-members-to-an-existing-enum/108962 "2024-01-19T19:33:39Z")

</div>

If there is an existing Enum with some members, e.g., @enum Fruit apple=1 orange=2 How could I later add kiwi=3 to it?

---

## [Coding style - upper or lower case for enum elements?](https://discourse.julialang.org/t/coding-style-upper-or-lower-case-for-enum-elements/107077)

<div class="topic-metadata">

**Author:** [@greatpet](https://discourse.julialang.org/u/greatpet)\
**Replies:** 0\
**Last updated:** [December 3, 2023, 2:25pm UTC](https://discourse.julialang.org/t/coding-style-upper-or-lower-case-for-enum-elements/107077 "2023-12-03T14:25:43Z")

</div>

The Julia manual for enum has the example @enum Fruit apple=1 orange=2 kiwi=3 According to some style guides for Julia, global constants should be UPPER\_CASE, so should I write @enum Fruit APPLE=1 ORANGE=2 KIWI=3 ins…

---

## [ANN: EnumX.jl -- improved enums for Julia](https://discourse.julialang.org/t/ann-enumx-jl-improved-enums-for-julia/78096)

<div class="topic-metadata">

**Author:** [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Replies:** 11\
**Last updated:** [November 5, 2023, 12:50pm UTC](https://discourse.julialang.org/t/ann-enumx-jl-improved-enums-for-julia/78096 "2023-11-05T12:50:55Z")

</div>

I wrote a small package EnumX that I want to introduce. Julia has builtin support for enums with the @enum macro, but such enums have some drawbacks, where the namespace/scope of the instances is the most annoying. This…

---

## [Design pattern with enums with some overlapping elements?](https://discourse.julialang.org/t/design-pattern-with-enums-with-some-overlapping-elements/104363)

<div class="topic-metadata">

**Author:** [@myersm0](https://discourse.julialang.org/u/myersm0)\
**Replies:** 0\
**Last updated:** [September 28, 2023, 6:27pm UTC](https://discourse.julialang.org/t/design-pattern-with-enums-with-some-overlapping-elements/104363 "2023-09-28T18:27:33Z")

</div>

I would appreciate it if anyone has any advice on elegantly handling this design problem: I’m writing a package for visualization of some custom types with GLMakie for a specific domain. In the domain, there are several…

---

## [More comfortable enum?](https://discourse.julialang.org/t/more-comfortable-enum/100779)

<div class="topic-metadata">

**Author:** [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Replies:** 8\
**Last updated:** [June 25, 2023, 6:15pm UTC](https://discourse.julialang.org/t/more-comfortable-enum/100779 "2023-06-25T18:15:56Z")

</div>

What I would expect is, if I have an enum like: @enum Color begin red green blue end That if I type Color. \<TAB\>\<TAB\> that I get a list of colors to choose from. But this is not the case. Is there a way t…

---

## [Solving the drawbacks of @enum](https://discourse.julialang.org/t/solving-the-drawbacks-of-enum/74506)

<div class="topic-metadata">

**Author:** [@taotree](https://discourse.julialang.org/u/taotree)\
**Replies:** 15\
**Last updated:** [June 23, 2023, 8:12am UTC](https://discourse.julialang.org/t/solving-the-drawbacks-of-enum/74506 "2023-06-23T08:12:16Z")

</div>

So, @enum is causing me major problems: I can’t use the names I want to use because it puts the names directly in the namespace. I need to go back and forth between strings and enums easily. Override Base.show for enum…

---

## [Wanted: proper enum type and switch statement with pattern matching on enumerable types](https://discourse.julialang.org/t/wanted-proper-enum-type-and-switch-statement-with-pattern-matching-on-enumerable-types/85225)

<div class="topic-metadata">

**Author:** [@Steven\_Sagaert](https://discourse.julialang.org/u/Steven_Sagaert)\
**Replies:** 5\
**Last updated:** [April 27, 2023, 9:57pm UTC](https://discourse.julialang.org/t/wanted-proper-enum-type-and-switch-statement-with-pattern-matching-on-enumerable-types/85225 "2023-04-27T21:57:08Z")

</div>

I’ve returned to Julia development after a break of a couple of years and I was suprised that there still not a proper enum type or a switch (or match if you like Scala) statement. The current enum with @enum is not goo…

---

## [Retrieving an instance of an enum using a string](https://discourse.julialang.org/t/retrieving-an-instance-of-an-enum-using-a-string/61183)

<div class="topic-metadata">

**Author:** [@lewis](https://discourse.julialang.org/u/lewis)\
**Replies:** 6\
**Last updated:** [April 8, 2023, 3:19am UTC](https://discourse.julialang.org/t/retrieving-an-instance-of-an-enum-using-a-string/61183 "2023-04-08T03:19:50Z")

</div>

This has been posed, sort of, several times in Discourse or stackoverflow. For this example: @enum condition nil=5 mild sick severe It is easy to get the enum with an integer: julia\> condition(5) nil::condition = 5 I…

---

## [Best way to select implementation based on runtime argument? (dispatch vs. control flow)](https://discourse.julialang.org/t/best-way-to-select-implementation-based-on-runtime-argument-dispatch-vs-control-flow/95479)

<div class="topic-metadata">

**Author:** [@lmtzx9h4qqnt](https://discourse.julialang.org/u/lmtzx9h4qqnt)\
**Replies:** 10\
**Last updated:** [March 6, 2023, 7:40pm UTC](https://discourse.julialang.org/t/best-way-to-select-implementation-based-on-runtime-argument-dispatch-vs-control-flow/95479 "2023-03-06T19:40:31Z")

</div>

I’ve been running into the following question: What is the most “sensible” or idiomatic way to select a function implementation based on a runtime argument (by which I mean, it could potentially be passed e. g. as a comm…

---

## [How to pick random values from an \`Enum\` type?](https://discourse.julialang.org/t/how-to-pick-random-values-from-an-enum-type/90330)

<div class="topic-metadata">

**Author:** [@singularitti](https://discourse.julialang.org/u/singularitti)\
**Replies:** 1\
**Last updated:** [November 16, 2022, 8:11am UTC](https://discourse.julialang.org/t/how-to-pick-random-values-from-an-enum-type/90330 "2022-11-16T08:11:44Z")

</div>

For example, define type Ball: @enum Ball b=1 r=-1 julia\> rand(Ball, 10) ERROR: ArgumentError: Sampler for this object is not defined Stacktrace: \[1\] Random.Sampler(#unused#::Type{Random.TaskLocalRNG}, sp::Random.Samp…

---

## [Enum type as field value performance](https://discourse.julialang.org/t/enum-type-as-field-value-performance/88867)

<div class="topic-metadata">

**Author:** [@HashBrown](https://discourse.julialang.org/u/HashBrown)\
**Replies:** 1\
**Last updated:** [October 17, 2022, 7:49pm UTC](https://discourse.julialang.org/t/enum-type-as-field-value-performance/88867 "2022-10-17T19:49:22Z")

</div>

I know it is discouraged to use an abstract type for a field in a composite type due to the runtime overhead of looking up the type and method resolution. Does using an enum type suffer from the same problem? Or is it m…

---

## [How to write a macro to export all instances of an enum type](https://discourse.julialang.org/t/how-to-write-a-macro-to-export-all-instances-of-an-enum-type/73137)

<div class="topic-metadata">

**Author:** [@paalon](https://discourse.julialang.org/u/paalon)\
**Replies:** 2\
**Last updated:** [September 22, 2022, 8:45am UTC](https://discourse.julialang.org/t/how-to-write-a-macro-to-export-all-instances-of-an-enum-type/73137 "2022-09-22T08:45:58Z")

</div>

A module has an enum type A and I want to export all its instances instances(A). I want to write a macro @export\_instances like module MyModule export A @enum A a b c @export\_instances A # \`export a, b, c\` end # module…

---

## [Surprising behaviour of @enum conversion from Integer types](https://discourse.julialang.org/t/surprising-behaviour-of-enum-conversion-from-integer-types/23556)

<div class="topic-metadata">

**Author:** [@speps](https://discourse.julialang.org/u/speps)\
**Replies:** 0\
**Last updated:** [April 26, 2019, 1:30pm UTC](https://discourse.julialang.org/t/surprising-behaviour-of-enum-conversion-from-integer-types/23556 "2019-04-26T13:30:45Z")

</div>

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: MethodEr…

---

## [Why does the first enum member value default to zero instead of one?](https://discourse.julialang.org/t/why-does-the-first-enum-member-value-default-to-zero-instead-of-one/87219)

<div class="topic-metadata">

**Author:** [@mthelm85](https://discourse.julialang.org/u/mthelm85)\
**Replies:** 3\
**Last updated:** [September 13, 2022, 6:13pm UTC](https://discourse.julialang.org/t/why-does-the-first-enum-member-value-default-to-zero-instead-of-one/87219 "2022-09-13T18:13:57Z")

</div>

I’m just curious since Julia uses 1-based indexing. E.g. julia\> @enum Color Red Blue julia\> Red Red::Color = 0 julia\> Blue Blue::Color = 1

---

## [@enum multiplication](https://discourse.julialang.org/t/enum-multiplication/85213)

<div class="topic-metadata">

**Author:** [@Lincoln\_Hannah](https://discourse.julialang.org/u/Lincoln_Hannah)\
**Replies:** 12\
**Last updated:** [August 7, 2022, 11:04pm UTC](https://discourse.julialang.org/t/enum-multiplication/85213 "2022-08-07T23:04:36Z")

</div>

Is this the best way to do this? @enum BuySell (Buy=1; Sell=-1) import Base.\* \*(x::BuySell, y::Any ) = Int(x) \* y \*(y::Any, x::BuySell) = y \* Int(x) Sell \* 10 # = -10 So I can put \* Buy o…

---

## [How can I determine the name of an enum value?](https://discourse.julialang.org/t/how-can-i-determine-the-name-of-an-enum-value/82394)

<div class="topic-metadata">

**Author:** [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Replies:** 3\
**Last updated:** [June 9, 2022, 1:55am UTC](https://discourse.julialang.org/t/how-can-i-determine-the-name-of-an-enum-value/82394 "2022-06-09T01:55:53Z")

</div>

How can I get the name of an enum value? @enum Animal dog cat Int(cat) # works String(cat) # not working How can I get the string representation of a variable of type Animal? I want to get the string “cat” or “dog”…

---

## [ProtoBuf Enum - NamedTuple or Base.Enums.Enum type?](https://discourse.julialang.org/t/protobuf-enum-namedtuple-or-base-enums-enum-type/80285)

<div class="topic-metadata">

**Author:** [@FireCrumb](https://discourse.julialang.org/u/FireCrumb)\
**Replies:** 1\
**Last updated:** [May 11, 2022, 4:31am UTC](https://discourse.julialang.org/t/protobuf-enum-namedtuple-or-base-enums-enum-type/80285 "2022-05-11T04:31:52Z")

</div>

Hello, I have a enum type in Protocol Buffer (ProtoBuf) enum Fruit { APPLE=1; ORANGE=2; KIWI=3; }; I want to load and use it in Julia. Searched online and arrived to ProtoBuf.jl package. In the documenta…

---

## [Call @enumx macro with NamedTuple constant as input](https://discourse.julialang.org/t/call-enumx-macro-with-namedtuple-constant-as-input/80699)

<div class="topic-metadata">

**Author:** [@FireCrumb](https://discourse.julialang.org/u/FireCrumb)\
**Replies:** 1\
**Last updated:** [May 9, 2022, 11:43am UTC](https://discourse.julialang.org/t/call-enumx-macro-with-namedtuple-constant-as-input/80699 "2022-05-09T11:43:21Z")

</div>

Hi, I have a NamedTuple and would like to create an EnumX from it, how can this be done? CC @fredrikekre Specifically, I’m using ProtoBuf.il to load an enum from Protocol Buffers. And I want to create a new type using…

---

## [Why can't Enums be #undef?](https://discourse.julialang.org/t/why-cant-enums-be-undef/80635)

<div class="topic-metadata">

**Author:** [@janneshb](https://discourse.julialang.org/u/janneshb)\
**Replies:** 8\
**Last updated:** [May 6, 2022, 11:14pm UTC](https://discourse.julialang.org/t/why-cant-enums-be-undef/80635 "2022-05-06T23:14:40Z")

</div>

When trying to initialize Enum as #undef it instead goes into an invalid state by adopting an arbitrary integer. Consider the following example: julia\> @enum Fruit Apple Banana Peach julia\> struct Basket{T} …

---

## [Vector to @enum](https://discourse.julialang.org/t/vector-to-enum/79907)

<div class="topic-metadata">

**Author:** [@clrescobar](https://discourse.julialang.org/u/clrescobar)\
**Replies:** 8\
**Last updated:** [April 25, 2022, 8:23am UTC](https://discourse.julialang.org/t/vector-to-enum/79907 "2022-04-25T08:23:48Z")

</div>

Hello, I would like to know how to convert a vector into @enum. I have tried: AS =\[Action(τ,ρ,χ,ω) for τ in 0:5 for ρ in 0:5 for χ in 0:5 for ω in 0:5\] for i in 1:1296 action = AS\[i\] @enum Actions action end B…

---

## [Dispatch on enum](https://discourse.julialang.org/t/dispatch-on-enum/77398)

<div class="topic-metadata">

**Author:** [@Impressium](https://discourse.julialang.org/u/Impressium)\
**Replies:** 7\
**Last updated:** [March 4, 2022, 5:17pm UTC](https://discourse.julialang.org/t/dispatch-on-enum/77398 "2022-03-04T17:17:40Z")

</div>

Hey all :slight\_smile: Is it possible to dispatch a function on different enum instances. Something like this: julia\> @enum E a b julia\> f(::Type{a}) = :a julia\> f(::Type{b}) = :b julia\> f(a) :a

---

## [Enum not at top level expression](https://discourse.julialang.org/t/enum-not-at-top-level-expression/23119)

<div class="topic-metadata">

**Author:** [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Replies:** 7\
**Last updated:** [December 2, 2021, 1:03am UTC](https://discourse.julialang.org/t/enum-not-at-top-level-expression/23119 "2021-12-02T01:03:57Z")

</div>

Hello guys! I have something like this: So basically an enum array, where a specific enum like “Points” refers to a string and type “POINTS” / Float32. My question is: If I try putting this in the main function I g…

---

## [Broadcast comparison of array of enum objects gives an error](https://discourse.julialang.org/t/broadcast-comparison-of-array-of-enum-objects-gives-an-error/68146)

<div class="topic-metadata">

**Author:** [@pjgorski](https://discourse.julialang.org/u/pjgorski)\
**Replies:** 11\
**Last updated:** [September 15, 2021, 1:56pm UTC](https://discourse.julialang.org/t/broadcast-comparison-of-array-of-enum-objects-gives-an-error/68146 "2021-09-15T13:56:24Z")

</div>

I have an array of @Enum objects. I want to count the array elements having a certain value. @enum E x = 0 y = 1 Es = \[E(0) for i in 1:100\] sum(Es .== E(0)) Above code and just Es .== E(0) gives the following error: E…

[Next page](https://discourse.julialang.org/tag/enum/1101.md?match_all_tags=true&page=1&tags%5B%5D=enum)
