Skip to content

Snippets

tim-hardcastle edited this page Sep 27, 2023 · 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. 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 them! 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 | sign to delimit bits of Charm which should be interpreted by the calling context. However, | has no semantic or syntactic significance and was chosen because it means nothing in Charm and so is a spare symbol. It is therefore a useful convention but not a necessity.

🧿 Pipefish

Clone this wiki locally