Wiki

Enumeration

An enumeration is a distinct type consisting of a set of named constants or symbolic values (These are also sometimes known as an enumerator list or enumeration elements).
On platforms that support it and where the implementation supports typing, the underlying type of the enumeration elements is int and the first default enumeration element has the value 0 with the value of each successive enumerator increased by 1.

Elements can have initialisers to override the default values if this is desired (or supported).

In Cobra an enumeration is declared with the enum keyword followed by the enumeration name.
As this is a type the enum name must start with an uppercase letter.
Enumeration element names (with optional initialiser values) follow individually on subsequent lines each equally indented or with multiple values, comma separated on the same line.

enum States
    Init
    Valid
    Working
    Dead

enum Tasks
    ChopWood, CarryWater
    Enlightenment

Emumerations are referenced by the Enumeration class name dotted with the enumeration element name
or like a constructor call: enumName(enumElementName) .

e = MyEnum.Start
e1 = MyEnum(Start)

Multiple elements can be collected together in a set by specifying multiple elementNames comma separated within the constructor-like call.

enum IsNames
  public, protected, private
  shared
  virtual, new, override, abstract
...

    mainMethod.isNames = IsNames(public,shared)

Grammar

enum <enumName> [ is <accessModifier>] 
    [ has <Attributes>] 
    [ of <StorageType> ]
    [ <DocString> ]
    <enumItemName> [ = <value> ]
    ...

    e = <enumName>.<enumItemName>
    e1 = <enumName>(<enumItemName>[, <enumItemName> ...])

Examples

enum Days 
    """In this enumeration, (on .Net) Sat is 0, Sun is 1, Mon is 2, and so forth.""" 
    Sat
    Sun
    Mon
    Tue
    Wed
    Thu
    Fri

class UseDays
    def main is shared
        today = Days.Fri
        yesterday = Days(Thu)
        weekend = Days(Sat, Sun)

        isWeekend = today in [Days.Sat, Days.Sun] # explicit test for enum member inclusion


enum OtherDays 
    Mon, Tue, Wed, Thu, Fri
    Sat, Sun

Platform

Explicit typing (StorageType) of enumeration elements and bitwise operations on elements are only supported on platforms that implement enums as variants of (integer) numeric Types (.Net)

For maximum portability and readability avoid using C-like bitpattern initialiser values and bitwise math operations for set inclusion and instead use either a direct equality comparison or the set inclusion form for testing enum member inclusion.

enum CarOptions 
   None, SunRoof, Spoiler, FogLights, TintedWindows 

 if myOption == CarOptions.FogLights, ...

 sportOptions = CarOptions(SunRoof, Spoiler)
 .. 
 assert CarOptions.SunRoof in sportOptions

 myOption = CarOption.SunRoof
 assert myOption in sportOptions
 

On .Net note the effect of System.FlagsAttribute on an enum

enum CarOptions 
    has Flags
    SunRoof = 0x01
    Spoiler = 0x02
    FogLights = 0x04
    TintedWindows = 0x08

class FlagTest
    def main is shared
        options = CarOptions(SunRoof,FogLights)
        print options
        print options to int
        assert (options & CarOptions.SunRoof) == CarOptions.SunRoof  # bitwise test combined options
        # above same as   assert CarOptions.SunRoof in options

/# Output is
    SunRoof, FogLights
    5
   Without 'has Flags' on enum CarOptions
    5
    5
#/    

Notes

It is suggested (where typed and initialised) all enumerations have an element whose value is 0.
If this is in addition to the default starting point of values it should be named None.

See Also

 enum(C# Reference)

Back to LanguageTopics