-
Notifications
You must be signed in to change notification settings - Fork 4
Enums
tim-hardcastle edited this page Jul 30, 2024
·
8 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)
isBlack(suit Suit) : suit in {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.
- Getting started
- Language basics
- The type system and built-in functions
- Functional Pipefish
- Encapsulation
- Imperative Pipefish
-
Imports and libraries
- The crypto/aes library
- The crypto/bcrypt library
- The crypto/rand library
- The crypto/rsa library
- The crypto/sha_256 library
- The crypto/sha_512 library
- The database/sql library
- The encoding/base_32 library
- The encoding/base_64 library
- The encoding/csv library
- The encoding/json library
- The files library
- The fmt library
- The html library
- The image library
- The image/bmp library
- The image/color library
- The image/jpeg library
- The image/png library
- The lists library
- The markdown library
- The math library
- The math/big library
- The math/cmplx library
- The math/rand library
- The net/http library
- The net/mail library
- The net/smtp library
- The net/url library
- The os/exec library
- The path library
- The path/filepath library
- The reflect library
- The regexp library
- The strconv library
- The strings library
- The terminal library
- The time library
- The unicode library
- Advanced Pipefish
- Developing in Pipefish
- Deployment
- Appendices