Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Runtime polymorphism can be achieved easily with enum’s variants. All variants of an enum have the same enum type and thus can be put together in the same collection. Each time an enum’s value must be processed, the compiler will check if we have taken into account all defined variants. This is particularly useful when adding new variants to an enum.

As an example, let us suppose we want to store key/value pairs inside a dictionary. The keys are all strings, but values are either signed integers, boolean, floats or strings. To achieve this, we may define the following enum type:

Then we can fill a dictionary using this enum:

When getting a value from the dictionary, we need to process each variant:

Integer value: 10
()