Enums

From Adam Colton
Revision as of 16:21, 17 September 2019 by Adamcolton (talk | contribs)
Jump to navigation Jump to search

Basic version is a list.

Basic List

 type Terrain enum [
   Wall
   Floor
   Rock
 ]

Will give Wall a value of 0, Floor=1 and Rock=2. It does string conversion

 Terrain.Wall.String() // "Wall"
 Terrain.Parse("Wall") // Null<Terrain> of Terrain.Wall
 Terrain.Parse("Foo")  // Null<Terrain> of Null

Flag

 type Permission flag [
   Read
   Write
   Execute
 ]

This will give the values Read=1, Write=2, Execute=4

Compound

 type Door enum {
   IsOpen bool
   IsLocked bool
 }[
   Closed
   Open  : IsOpen
   Locked: IsLocked
 ]

This prevents the invalid state of open and locked. It will also assign the values Closed=0 open=1 locked=2 matching the bit pattern. It allows for matching

 d := Door.Closed
 Door.IsOpen(d) // false
 d = .Locked
 Door.IsOpen(d) // true