#08. Functions
In Telepact, a function is an argument struct plus a result union.
#Start the demo server
telepact demo-server --port 8000#Read a function definition
Here is fn.evaluate:
{
"fn.evaluate": {
"expression": "union.Expression"
},
"->": [
{
"Ok_": {
"result": "number",
"saveResult": "fn.saveVariable"
}
},
{
"ErrorUnknownVariables": {
"unknownVariables": ["string"]
}
},
{
"ErrorCannotDivideByZero": {}
}
]
}The function entrypoint itself defines the argument struct. The -> entrypoint defines the result union.
Ok_ is always required. Everything else is treated as an error result.
#Links
Notice saveResult uses fn.saveVariable as a type expression. That is a link: the server is returning a prepopulated future call shape.
Let's see it:
curl -s localhost:8000/api -d '[{}, {"fn.evaluate": {"expression": {"Add": {"left": {"Constant": {"value": 2}}, "right": {"Constant": {"value": 4}}}}}}]'[
{},
{
"Ok_": {
"result": 6,
"saveResult": {
"fn.saveVariable": {
"name": "result",
"value": 6
}
}
}
}
]#Error cases
Unknown variable:
curl -s localhost:8000/api -d '[{}, {"fn.evaluate": {"expression": {"Variable": {"name": "missing"}}}}]'[{}, {"ErrorUnknownVariables": {"unknownVariables": ["missing"]}}]Divide by zero:
curl -s localhost:8000/api -d '[{}, {"fn.evaluate": {"expression": {"Div": {"left": {"Constant": {"value": 4}}, "right": {"Constant": {"value": 0}}}}}}]'[{}, {"ErrorCannotDivideByZero": {}}]#Optional fields in function-shaped definitions
Optional fields still use !, even inside function arguments:
{
"fn.getPaperTape": {
"limit!": "integer"
}
}So both of these are valid:
curl -s localhost:8000/api -d '[{}, {"fn.getPaperTape": {}}]'
curl -s localhost:8000/api -d '[{}, {"fn.getPaperTape": {"limit!": 1}}]'Next: 09. Service errors