Debian Codegraph @

doc/_includes/examples/functions.jsonnet

raw
1 // Define a local function.
2 // Default arguments are like Python:
3 local my_function(x, y=10) = x + y;
4
5 // Define a local multiline function.
6 local multiline_function(x) =
7 // One can nest locals.
8 local temp = x * 2;
9 // Every local ends with a semi-colon.
10 [temp, temp + 1];
11
12 local object = {
13 // A method
14 my_method(x): x * x,
15 };
16
17 {
18 // Functions are first class citizens.
19 call_inline_function:
20 (function(x) x * x)(5),
21
22 call_multiline_function: multiline_function(4),
23
24 // Using the variable fetches the function,
25 // the parens call the function.
26 call: my_function(2),
27
28 // Like python, parameters can be named at
29 // call time.
30 named_params: my_function(x=2),
31 // This allows changing their order
32 named_params2: my_function(y=3, x=2),
33
34 // object.my_method returns the function,
35 // which is then called like any other.
36 call_method1: object.my_method(3),
37
38 standard_lib:
39 std.join(' ', std.split('foo/bar', '/')),
40 len: [
41 std.length('hello'),
42 std.length([1, 2, 3]),
43 ],
44 }