Skip to content

2.5. Enums#

Syntax#


EnumDefinition = (* enum_keyword: *) ENUM_KEYWORD
(* name: *) IDENTIFIER
(* open_brace: *) OPEN_BRACE
(* members: *) EnumMembers
(* close_brace: *) CLOSE_BRACE;

EnumMembers = ((* item: *) IDENTIFIER ((* separator: *) COMMA (* item: *) IDENTIFIER)*)?;

Enum Types#

Enums can be used to create custom types with a finite set of constant values. Enums can be declared on the file level, outside of contract or library definitions.

enum ActionChoices {
    One,
    Two
}

contract MyContract {
    function choose() public pure returns (ActionChoices) {
        return ActionChoices.Two;
    }
}

Enums require at least one member, and its default value when declared is the first member. Enums cannot have more than 256 members.

Note

This section is under construction. You are more than welcome to contribute suggestions to our GitHub repository.