Skip to content
Brom Bresenham edited this page Oct 27, 2023 · 5 revisions

Syntax

enum Name [attributes]
  ALPHA
  BETA
  GAMMA
endEnum

println Name.ALPHA         # ALPHA
println Name.ALPHA.name    # ALPHA
println Name.ALPHA.value   # 0
println Name.ALPHA->Int32  # 0
println Name.ALPHA->String # ALPHA
println Name.categories    # [ALPHA,BETA,GAMMA]
println Name.ALPHA == Name("ALPHA")  # true

enum Name( x:Real64, y=-1:Int32 )
  CATEGORIES
    ALPHA(1.1) = 1
    BETA(2.2)
    GAMMA(3.3,3)

  METHODS
    method z->Int32
      which (this)
        case BETA: return 1
        others:    return 0
      endWhich
endEnum

println Name.ALPHA->Int32  # 1
forEach (category in Name.categories)
  println "$ x:$ y:$ z:$"(category,category.x,category.y,category.z)
  # ALPHA x:1.1 y:-1 z:0
  # BETA x:2.2 y:-1 z:1
  # GAMMA x:3.3 y:3 z:0
endForEach

enum Flags [bitflags]
  ALPHA, BETA, GAMMA
endEnum

local flags = Flags[ALPHA,GAMMA] # equivalent to: Flags.ALPHA | Flags.GAMMA
trace flags.is_alpha  # true
trace flags.is_beta   # false
flags.is_alpha = false
trace flags           # GAMMA

Description

Enums are fast and efficient enumerated types. Underneath the hood they are implemented using pass-by-value compounds consisting of a single Int32 value. Any additional properties that are used to define enum categories are implemented as getter methods; they are not stored directly with each enum value.

Attributes

Attribute Description
[bitflags] Define category values as powers of two - 1, 2, 4, 8, ... instead of 0, 1, 2, 3, .... A bitflags enum also defines bitwise operators &, |, and ! as well as access methods is_x for each category X.

Properties

Property Type Description
value Int32 The ordinal used to represent one of the enumerated categories.

Constructors

Constructor Return Type Description
EnumType(Int32) EnumType Creates an enumerated value from its ordinal.
EnumType(String) EnumType Creates an enumerated value from its String name.

Global Methods

Method Return Type Description
categories() EnumType[] Returns a list of every category defined in the enum.

Methods

Method Return Type Description
name() String Returns the name of the category.
operator?() Logical Returns true if the category's ordinal value is non-zero (if (enum_value?) ...).
operator==(other:EnumType) Logical Returns true if this enum's category matches the other (if (enum_value == Category.ALPHA).
to->Int32 Int32 Returns the category ordinal value.
to->String String Returns the category name.

[bitflags] Methods

Method Return Type Description
is_category() Logical Replicated for each bitflag category. E.g. an "Attributes" [bitflags] enum with categories BLOCKER and PUSHABLE will have methods is_blocker() and is_pushable().
set_is_category(Logical) nil Sets the desired category flag on an enum-type value. E.g. set_is_blocker(), set_is_pushable().
operator!() EnumType Returns an enum containing the bitwise complement of the original ordinal value.
operator&(other:EnumType) EnumType Returns the Bitwise-AND of two enums (if ((attributes & layer_mask)?) ...).
operator|(other:EnumType) EnumType Returns the Bitwise-OR of two enums.
Clone this wiki locally