Enumerations: Difference between revisions

added Factor
No edit summary
(added Factor)
Line 445:
For the unspecific value enum use case, Erlang has atoms. You can use apple, banana, orange directly in the code.
If they have to have a specific value they could be grouped like this: {apple, 1}, {banana, 3}, {orange, 8}
 
=={{header|Factor}}==
 
Enumerations are essentially association lists with values (keys) assigned sequentially from constants (values) provided by an initial sequence.
<lang factor>IN: scratchpad { "sun" "mon" "tue" "wed" "thur" "fri" "sat" } <enum>
 
--- Data stack:
T{ enum f ~array~ }
IN: scratchpad [ 1 swap at ] [ keys ] bi
 
--- Data stack:
"mon"
{ 0 1 2 3 4 5 6 }</lang>
Factor also provides C-like enumerations in its C library interface. These enumerations may have explicit values.
<lang factor>IN: scratchpad USE: alien.syntax
IN: scratchpad ENUM: day sun mon { tue 42 } wed thur fri sat ;
IN: scratchpad 1 <day>
 
--- Data stack:
mon
IN: scratchpad 42 <day>
 
--- Data stack:
mon
tue</lang>
 
=={{header|Fantom}}==
1,808

edits