Ad space (banner)
๐ŸŸฃJulia Lessons
Lesson 41 / 68

The basics of enums

This lesson covers the basics of Julia's @enum macro, with the aim of handling a fixed set of options under readable names.

The @enum macro creates an enumerated type representing a fixed set of options. Use it when you want to limit the possible values in advance — a traffic light is only ever red, green, or amber.

The sample code defines three options in the single line @enum Color Red Green Blue and assigns one to a variable as c = Red. Defining an enum in one concise line through a macro is characteristic of Julia.

A common early stumble is that an @enum is backed by integers underneath. It displays as a name, but consecutive integers starting at 0 are assigned internally — knowing that saves surprises.

In professional work, a named enum conveys intent far better than scattering strings or numbers through the code.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run Julia directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)