Skip to content

Snippets

Tim Hardcastle edited this page Feb 29, 2024 · 8 revisions

We have already met snippets, without noticing it, in the pages on SQL interop and microservices. Consider the following command.

add (name string, age int) :
    put SQL ---
        INSERT INTO People
        VALUES |name, age|

The --- operator is in fact a special kind of constructor. It forms a struct of type SQL with two fields, text and env. The text field contains the text after the ---, so in this case INSERT INTO People ... etc. And env contains a map in which the keys are the names of all the variables in scope (represented as strings) and the values are the values of the variables.

In order for this to work, SQL had to be declared as a language under the languages headword (this is done in the world.pf library, which is automatically imported). You can do this too, let your imagination run wild!

languages

Cpp
Python
Prolog

Of course that still leaves you with the difficulty of implementing the language! The snippet syntax is just a nice way of wrapping up and presenting you with the data you need to do that.

In both the SQL and microservice snippets, I have used the | symbol to delimit bits of Pipefish which should be interpreted by the calling context. However, | has no semantic or syntactic significance and was chosen because it means nothing in Pipefish and so is a spare symbol. It is therefore a useful convention but not a necessity.

🧿 Pipefish

Clone this wiki locally