Enums: Difference between revisions

From Adam Colton
Jump to navigation Jump to search
Created page with "Basic version is a list. Category:Wonderland Category:Gryphon"
 
No edit summary
Line 1: Line 1:
Basic version is a list.
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


[[Category:Wonderland]]
[[Category:Wonderland]]
[[Category:Gryphon]]
[[Category:Gryphon]]

Revision as of 16:21, 17 September 2019

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