-
Notifications
You must be signed in to change notification settings - Fork 6
Enums
tim-hardcastle edited this page Sep 23, 2024
·
7 revisions
Pipefish lets you create your own enumerated types.
The script examples/enums.pf
supplies an example of usage:
newtype
Suit = enum CLUBS, HEARTS, SPADES, DIAMONDS
Value = enum ACE, TWO, THREE, FOUR, FIVE, SIX, SEVEN,
.. EIGHT, NINE, TEN, JACK, QUEEN, KING
Card = struct(value Value, suit Suit)
def
isBlack(suit Suit) : suit in set CLUBS, SPADES
isBlack(card Card) : isBlack(card[suit])
The len
function can be applied to enum types, e.g. len Suit
is 4
; and similarly enum types can be indexed by integers, e.g. Suit[2]
is SPADES
.
Let's put it through its paces in the REPL:
→ hub run "examples/enums.pf" as "Enums"
Starting script 'examples/enums.pf' as service 'Enums'.
Enums → Card ACE, CLUBS
Card with (value :: ACE, suit :: CLUBS)
Enums → isBlack CLUBS
true
Enums → isBlack Card QUEEN, HEARTS
false
Enums → type DIAMONDS
Suit
Enums →
Note that two enums can't have a member in common.
🧿 Pipefish is distributed under the MIT license. Please steal my code and ideas.