This content is also available at learn.palantir.com ↗ and is presented here for accessibility purposes.
Your repository comes with sample Functions already written in the ../src/index.ts
file. Let's explore these and use them as the basis for dissecting and testing a "Hello World" example.
Open the ../src/index.ts
file by clicking on it in the Files panel on the left. Be sure not to select ../src/__tests__/index.ts
.
With the index.ts
file open, highlight the entire code block with ctrl+a
(or cmd+a
on a Mac) and hit the Delete key on your keyboard.
Copy and paste the code block below and paste it into your blank editor.
import { Function, Integer } from "@foundry/functions-api";
import { } from "@foundry/ontology-api";
export class MyFunctions {
@Function()
public myFunction(n: Integer): Integer {
return n + 1;
}
}
In these few lines of code, we can discern the basic structure of a Function:
Click the fx Functions tab in the helper toolbar along the bottom of your screen. In the Live preview tab of the functions helper, you can preview and test the functions in your repository. Before you test your functions, your repository’s Code Assist feature must be in a “running” state as indicated by the bar across the very bottom of your screen.
With Code Assist running and your functions helper open to the Live preview tab, click on the name of our sample function (“myFunction”).
n
in the form, because that's the variable name given in the function (i.e., (n: Integer)
).Replace the 0
in the n
field with an arbitrary integer and click the blue Run ▶
button to test the function.
Scroll down in the functions helper window to view the output, which should equal your n
value plus 1 as described in the function (i.e., { n+1 }
).
Typescript functions must have explicit type annotations on all input parameters and specify an explicit return type. It’s important therefore to be aware of the supported input and output types. Take a few moments to read the selections below from the Functions API documentation. You’ll have a chance to review the object types entries later. When reading, there’s no need to follow the links to ancillary documentation unless desired.