ENUM:
Factor handbook » C library interface » Passing data between Factor and C » Enumeration types

Next:define-enum ( word base-type members -- )


Vocabulary
alien.syntax

Syntax
ENUM: type words... ; ENUM: type < base-type words...


Inputs
None

Outputs
None

Word description

Warning
This word is part of Factor's C library interface, and not intended for use with Factor data. Factor has its own native Enumerations which can be created with <enumerated>.

Creates a c-type that boxes and unboxes integer values to symbols. A singleton is defined for each member word which allows generic dispatch on the enum's members. The base c-type can optionally be specified and defaults to int. A constructor word <type> is defined for converting from integers to singletons. The generic word enum>number converts from singletons to integers. Enum-typed values are automatically prettyprinted as their singleton words. Unrecognizing enum numbers are kept as numbers.

Examples
Here is an example enumeration definition:
ENUM: color_t red { green 3 } blue ;


The following expression returns true:
3 <color_t> [ green = ] [ enum>number 3 = ] bi and


Here is a version where the C-type takes a single byte:
ENUM: tv_peripherals_1 < uchar { appletv 1 } { chromecast 2 } { roku 4 } ;


The same as above but four bytes instead of one:
ENUM: tv_peripherals_4 < uint { appletv 1 } { chromecast 2 } { roku 4 } ;


We can define a generic and dispatch on it:
ENUM: tv_peripherals_4 < uint { appletv 1 } { chromecast 2 } { roku 4 } ; GENERIC: watch-device ( device -- ) M: appletv watch-device drop "watching appletv" print ; M: chromecast watch-device drop "watching chromecast" print ; appletv watch-device


See also
define-enum, enum>number, number>enum

Definition