-
Notifications
You must be signed in to change notification settings - Fork 4
Type conversion and reflection
Pipefish has a number of built-in functions for type conversion.
Usually they have the same name as the type they convert to:
float converts ints to float, or converts strings to floats if they have the right format.
int converts floats to integers by truncation, or converts strings to ints if they have the right format.
set will turn a tuple into a set.
string will convert anything to a string. This has been given some sensible defaults but can be overloaded for any type except tuple.
literal converts a value into a string which is the Pipefish literal of that value.
tuple will leave a tuple unchanged and turn anything else in to a tuple of length 1.
L ... will convert a list L into a tuple with the same elements.
To turn a tuple t into a list it is only necessary to wrap it in square brackets: [t].
type converts a value to its type.
cast <type>, <value> casts between types, specifically between clone types and their parent types and vice versa, between int and enum types, and vice-versa; and from lists of values to structs.
unsafe <type>, <value> casts from a parent type to its clone without running any type validation attached to the type. This obviously saves time, but is, as it says "unsafe", and should be used carefully while constructing the basic operations on a type in the library that declares it, not scattered willy-nilly in your code.
In the list above we briefly noted that type converts a value to its type. That is, type "walrus" evaluates to string, type 42 evaluates to int, etc.
string and int are themselves members of the type type. That is, type string returns type.
type is a perfectly normal Pipefish type itself, and belongs, of course, to type type. Try not to let this bother you.
We can also use the in keyword to check for type membership: so for example "walrus" in string and string in type are true.
🧿 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