@@ -25,8 +25,8 @@ will fail.
25
25
!SECTION Variables and side effects
26
26
27
27
User functions can take any number of input arguments and should
28
- provide one result. They should be kept purely functional and thus free of
29
- side effects and state, and state modification.
28
+ provide one result via a `return` statement. User functions should be kept
29
+ purely functional and thus free of side effects and state, and state modification.
30
30
31
31
Modification of global variables is unsupported, as is changing
32
32
the data of any collection from inside an AQL user function.
@@ -56,8 +56,8 @@ function (values) {
56
56
}
57
57
```
58
58
59
- The above function can be made free of side effects by using the `var`
60
- keyword , so the variables become function-local variables:
59
+ The above function can be made free of side effects by using the `var` or
60
+ `let` keywords , so the variables become function-local variables:
61
61
62
62
```js
63
63
function (values) {
@@ -71,14 +71,48 @@ function (values) {
71
71
}
72
72
```
73
73
74
+ !SECTION Input parameters
75
+
76
+ In order to return a result, a user function should use a `return` instruction
77
+ rather than modifying its input parameters.
78
+
79
+ AQL user functions are allowed to modify their input parameters for input
80
+ parameters that are null, boolean, numeric or string values. Modifying these
81
+ input parameter types inside a user function should be free of side effects.
82
+ However, user functions should not modify input parameters if the parameters are
83
+ arrays or objects and as such passed by reference, as that may modify variables
84
+ and state outside of the user function itself.
85
+
74
86
!SECTION Return values
75
87
76
88
User functions must only return primitive types (i.e. *null*, boolean
77
- values, numeric values, string values) or aggregate types (lists or
78
- documents ) composed of these types.
89
+ values, numeric values, string values) or aggregate types (arrays or
90
+ objects ) composed of these types.
79
91
Returning any other JavaScript object type from a user function may lead
80
92
to undefined behavior and should be avoided.
81
93
94
+ !SECTION Enforcing strict mode
95
+
96
+ By default, any user function code will not be executed in *strict mode* or
97
+ *strong mode*. In order to make a user function function being run in strict
98
+ mode, use `use strict` explicitly inside the user function, e.g.:
99
+
100
+ ```js
101
+ function (values) {
102
+ "use strict"
103
+
104
+ for (var i = 0; i < values.length; ++i) {
105
+ var name = values[i];
106
+ if (name === "foo") {
107
+ return i;
108
+ }
109
+ }
110
+ return null;
111
+ }
112
+ ```
113
+
114
+ Any violation of the strict mode will trigger a runtime error.
115
+
82
116
!SECTION Miscellaneous
83
117
84
118
Internally, user functions are stored in a system collection named
0 commit comments