Wiki

Changes between Version 4 and Version 5 of Enum

Show
Ignore:
Timestamp:
08/27/12 10:11:01 (12 years ago)
Author:
hopscc
Comment:

Correct errors and tweak slightly to accomodate Java BE

Legend:

Unmodified
Added
Removed
Modified
  • Enum

    v4 v5  
    22 
    33An enumeration is a distinct type consisting of a set of named constants (enumerator list, enumeration elements). [[BR]] 
    4 By default the underlying type of the enumeration elements is int and the first enumerator 
    5 has the value 0 with the value of each successive enumerator increased by 1.[[BR]]  
    6 Enumerators can have initialisers to override the default values. 
     4On platforms that support it and where typed, the underlying type of the enumeration elements is int and the first enumeration element has the value 0 with the value of each successive enumerator increased by 1.[[BR]]  
     5 
     6Elements can have initialisers to override the default values if this is desired. 
    77 
    88In Cobra an enumeration is declared with the '''enum''' keyword followed by the enumeration name.[[BR]] 
     
    3535Multiple elements can be or'd together by specifying multiple elementNames comma separated  
    3636within the constructor-like call. 
     37{{{ 
     38#!cobra 
     39enum IsNames 
     40  public, protected, private 
     41  shared 
     42  virtual, new, override, abstract 
     43... 
     44 
     45    mainMethod.isNames = IsNames(public,shared) 
     46}}} 
    3747 
    3848== Grammar == 
     
    4050enum <enumName> [ is <accessModifier>]  
    4151    [ has <Attributes>]  
    42     [ of <Type> ] 
     52    [ of <StorageType> ] 
    4353    [ <DocString> ] 
    4454    <enumItemName> [ = <value> ] 
     
    4959}}} 
    5060 
    51 == Platform == 
    5261 
    5362== Examples == 
     
    5564#!cobra 
    5665enum Days  
    57     """In this enumeration, Sat is 0, Sun is 1, Mon is 2, and so forth."""  
     66    """In this enumeration, (on .Net) Sat is 0, Sun is 1, Mon is 2, and so forth."""  
    5867    Sat 
    5968    Sun 
     
    6776    def main is shared 
    6877        today = Days.Fri 
    69         yesterday = Day(Thu) 
     78        yesterday = Days(Thu) 
    7079        weekend = Days(Sat, Sun) 
    7180 
    72         isWeekend = today in [Days.Sat, Days.Sun] # explicit test enum member inclusion 
     81        isWeekend = today in [Days.Sat, Days.Sun] # explicit test for enum member inclusion 
    7382 
    7483 
     
    7988 
    8089== Platform == 
    81 On .Net see the effect of System.!FlagsAttribute on an enum  
     90Explicit typing (!StorageType) of enumeration elements and bitwise operations on elements are only supported on platforms that implement enums as variants of numeric Types (.Net) 
     91 
     92For maximum portability and readability try and avoid using initialiser values and use either a direct equality comparison  
     93or the set inclusion form for testing enum member inclusion.  
     94{{{ 
     95#!cobra 
     96 if today == Day.Fri, ... 
     97 
     98 assert CarOptions.SunRoof in options 
     99}}}  
     100 
     101 
     102On .Net note the effect of System.!FlagsAttribute on an enum  
    82103{{{ 
    83104#!cobra 
     
    106127 
    107128== Notes == 
    108 It is suggested all enumerations have an element whose value is 0.[[BR]]  
     129It is suggested (where typed and initialised) all enumerations have an element whose value is 0.[[BR]]  
    109130If this is in addition to the default starting point of values it should be named '''None'''. 
    110131