-
Notifications
You must be signed in to change notification settings - Fork 4
Structs
In this page we will discuss the struct type, its constructors, and its operations.
As an example, consider the file `examples/struct.ch".
def
Person = struct(name string, age int)
Cat = struct(name string, nobelPrizes int, pink bool)
CatOwner = struct(name string, pet Cat)
CAT_DEFAULTS = nobelPrizes::0, pink::false
var
doug = Person "Douglas", 42
joe = Person with name::"Joseph", age::22
tom = Person with age::49, name::"Thomas"
myCat = Cat with name::"Felix", CAT_DEFAULTS
me = CatOwner("Tim", myCat)
myField = name
The struct type is defined in the def section of the script, as for example Person = struct(name string, age int). This creates the Person type and specifies the types of its fields. It also supplies us with a "short constructor" of the form Person <name>, <age>, and a "long constructor" of the form Person with name::<name>, age::<age>, where the fields come in any order.
User-defined types should be named in PascalCase.
Let's demonstrate in the REPL that it does what you think it would do.
→ hub run "examples/struct.ch" as "Struct"
Starting script 'examples/struct.ch' as service 'Struct'.
Struct → doug
Person with (name::Douglas, age::42)
Struct → me
CatOwner with (name::Tim, pet::Cat with (name::Felix, nobelPrizes::0, pink::false))
Struct →
Structs are indexed using square brackets, just like everything else in Charm. The fields of structs are first-class objects. Let's demonstrate in the REPL:
Struct → doug[age]
42
Struct → me[pet][pink]
false
Struct → tom[myField]
Thomas
Struct →
In common with the list, map, and tuple types, we can use the with keyword to produce an altered copy of a struct:
Struct → doug with age::43
Person with (name::Douglas, age::43)
Struct → me with [pet, pink]::true
CatOwner with (name::Tim, pet::Cat with (name::Felix, nobelPrizes::0, pink::true))
Struct → joe with name::"Josephine", age::23
Person with (name::Josephine, age::23)
Struct →
🧿 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