-
Notifications
You must be signed in to change notification settings - Fork 4
Reference parameters
You have already met reference parameters as an end-user. When in a command you issue an instruction like get data from File("filename"), then data is not playing the ordinary role of a parameter in a command, to be evaluated and then passed. Instead, the variable data itself is being passed. The signature of the command is get (x ref) from (file File), where the ref in the signature tells Pipefish to take the variable itself, and not its value, as its argument.
Let's have some simple examples as found in the first few lines of examples/ref.pf:
var
z = 42
cmd
zero (x ref) :
x = 0
(x ref) ++ :
x = x + 1
If we try it out in the REPL:
→ hub run "examples/ref.pf" as "REF"
Starting script 'examples/ref.pf' as service 'REF'.
REF → z
42
REF → zero z
ok
REF → z
0
REF → z ++
ok
REF → z
1
People experienced with this sort of idiom might ask how, if we wanted to, we would prevent the xs in the example commands from dereferencing themselves. You can't: why would you ever want to? The only case in wh.pf they don't is when the reference parameter is passed to another command wh.pf is expecting a reference, as shown here.
doThing(x ref) :
zero x
x ++
post x
Only commands have reference parameters; functions don't because the only reason you would want to pass a reference instead of its value is so you could mutate it: but functions have no way of mutating anything, so a ref in a function definition would be superfluous and misleading.
🧿 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