Enum
Enumerated types, often called as enums, are a special kind of class used to represent a fixed number of constant values.
Declaring simple enums
DartPoet handles the creation from enums over the ClassSpec
with a specific builder method.
Results in the following code:
Declaring enhanced enums
The programming languages Dart also allow the declaration from enhanced enums. It can contain fields, methods and const constructors which are limited to a fixed number of known constant instances.
But the creation of enhanced enums contains some special requirements that are important to know:
Instance variables must be final, including those added by mixins.
All generative constructors must be constant.
Factory constructors can only return one of the fixed, known enum instances.
No other class can be extended as Enum is automatically extended.
There cannot be overrides for index, hashCode, the equality operator ==.
A member named values cannot be declared in an enum, as it would conflict with the automatically generated static values' getter.
All instances of the enum must be declared in the beginning of the declaration, and there must be at least one instance declared.
Now let's create an enhanced enum. The involved code for this is similar to the creation of a simple enum, but with some additional function calls: We reuse the code from the simple enum and add the required functions call to retrieve the enhanced enum.
When you add the parameter entries to an EnumProperty
, you can do it over two different ways. The first is you use a CodeBlock
which contains the necessary content or use the parameter method which takes a specific format string. For more information about the allowed formatting, take a quick look at the Placeholders page.
Which results in the following code: