Skip to content

Snippets

tim-hardcastle edited this page Aug 1, 2024 · 7 revisions

We have already met snippets, without noticing it, in the page SQL interop. 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 of type string and data of type list. The text field contains the raw text after the ---, so in this case INSERT INTO People ... etc. And data contains a list containing alternately the plain strings in the raw text and the values the expressions within bars evaluate to.

To create a snippet type, declare it under the newtypes headword.

newtype

Cpp = snippet
Python = snippet
Prolog = snippet

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 the case of SQL, Pipefish has functions in the (automatically imported) world library which mean that put(x SQL), post(x SQL) and delete(x SQL) are defined for you and know about the configuration of your SQL server.

There is also a ready-to-use HTML snippet. The post command is defined for it such that it will simply turn the data into a single string of text, using the built-in (and possibly overloaded) string function to turn the individual elements of the data list into text and concatenating them together.

Clone this wiki locally