-
Notifications
You must be signed in to change notification settings - Fork 4
Clone types
Besides the other user-defined types, it can be useful to have types which resemble, but are distinct from, the built-in types. If, for example, we use an integer to represent a UID, then it is convenient to declare a UID type which can be distinguished from other integers. Or it may be convenient to have an Apples type which can't be added to Oranges, or a vector type which can't be mistaken for a list:
We should explain about the using clause. Some operations, such as < for clones of integers or len for clones of lists, are supplied automatically when you declare the type, and work just the same as for the parent type. Others, such as + for integers or slicing for lists, must be explicitly requested in a using clause.
The underlying rule is that an operation must be requested if it would be expected to return a value in the clone type. Hence the operations that need requesting are +, -, *, /, div, mod,with, without, the operators >> and ?> for lists, and slicing clones of strings and lists. (Request the slicing operation with the word slice, the other operations by their names or symbols.)
An operation which you don't request in the using clause may still be overloaded by hand, if you don't want the operation to work like it does on the parent type. Here for example is how we'd implement addition for the Vec type. Don't worry if you don't quite understand it: it uses language features you haven't met yet, and is here just to show that you can do so when you need to.
As with structs and enums, each clone type has a constructor function with the same name as the type.
Let's demonstrate all this in the TUI.
🧿 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