1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
Function declarations follow the ECMAScript 5 syntax. Functions can be
declared anonymously, which is useful for "throw-away" functions such
as sort or filter callbacks or for building objects or arrays of function
values.
If functions are declared with a name, the resulting function value is
automatically assigned under the given name to the current scope.
When function values are stringifed, the resulting string will describe
the declaration of the function.
Nesting function declarations is possible as well.
-- Expect stdout --
function() { ... }
function test_fn(a, b) { ... }
function test2_fn(a, b) { ... }
A function declaration using the alternative syntax:
The function was called with arguments 123 and 456.
-- End --
-- Testcase --
{%
// declare an anonymous function and
// assign resulting value
anon_fn = function() {
return "test";
};
// declare a named function
function test_fn(a, b) {
return a + b;
}
// nesting functions is legal
function test2_fn(a, b) {
function test3_fn(a, b) {
return a * b;
}
return a + test3_fn(a, b);
}
print(anon_fn, "\n");
print(test_fn, "\n");
print(test2_fn, "\n");
%}
A function declaration using the alternative syntax:
{% function test3_fn(a, b): %}
The function was called with arguments {{ a }} and {{ b }}.
{% endfunction %}
{{ test3_fn(123, 456) }}
-- End --
Additionally, utpl implements ES6-like "rest" argument syntax to declare
variadic functions.
-- Expect stdout --
function non_variadic(a, b, c, d, e) { ... }
[ 1, 2, 3, 4, 5 ]
function variadic_1(a, b, ...args) { ... }
[ 1, 2, [ 3, 4, 5 ] ]
function variadic_2(...args) { ... }
[ [ 1, 2, 3, 4, 5 ] ]
-- End --
-- Testcase --
{%
// ordinary, non-variadic function
function non_variadic(a, b, c, d, e) {
return [ a, b, c, d, e ];
}
// fixed amount of arguments with variable remainder
function variadic_1(a, b, ...args) {
return [ a, b, args ];
}
// only variable arguments
function variadic_2(...args) {
return [ args ];
}
print(join("\n", [
non_variadic,
non_variadic(1, 2, 3, 4, 5),
variadic_1,
variadic_1(1, 2, 3, 4, 5),
variadic_2,
variadic_2(1, 2, 3, 4, 5)
]), "\n");
%}
-- End --
|