:@enum

..  @enum EnumName EnumValue1[=x] EnumValue2[=y]

Create an :obj:`Enum` type with name ``EnumName`` and enum member values of ``EnumValue1`` and ``EnumValue2`` with optional assigned values of ``x`` and ``y``, respectively. ``EnumName`` can be used just like other types and enum member values as regular values, such as

.. doctest::

   julia> @enum FRUIT apple=1 orange=2 kiwi=3

   julia> f(x::FRUIT) = "I'm a FRUIT with value: $(Int(x))"
   f (generic function with 1 method)

   julia> f(apple)
   "I'm a FRUIT with value: 1"

Examples

The @enum macro in Julia is used to create an Enum type with specified enum member values. It allows you to define a custom enumeration type with associated values for each member. Here are some examples of its usage:

julia> @enum FRUIT apple=1 orange=2 kiwi=3

This example creates an Enum type named FRUIT with three enum member values: apple, orange, and kiwi. Each member is assigned a specific value.

julia> f(x::FRUIT) = "I'm a FRUIT with value: $(Int(x))"
f (generic function with 1 method)

julia> f(apple)
"I'm a FRUIT with value: 1"

In this example, a function f is defined that takes an argument of type FRUIT. It demonstrates how to use the enum member values as regular values in Julia code.

Note: The x::FRUIT syntax in the function signature ensures that only values of type FRUIT can be passed to the function.

It's important to remember that the @enum macro is a code-generation tool and not a function. It creates a new type and its associated values during compilation.

See Also

User Contributed Notes

Add a Note

The format of note supported is markdown, use triple backtick to start and end a code block.

*Required Field
Details

Checking you are not a robot: