MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 1 | SYNOPSIS |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 2 | mixed apply(closure cl, ...) |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 3 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 4 | DESCRIPTION |
| 5 | Evaluates the closure <cl> with the following arguments. |
| 6 | If the last argument is an array or struct, it will be |
| 7 | flattened: ie. the array/struct itself will be removed and its |
| 8 | contents added to the argument list of <cl> |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 9 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 10 | If <cl> is not a closure, it will simply be returned (and all |
| 11 | other arguments are ignored). |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 12 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 13 | EXAMPLES |
| 14 | The flattening of the last argument is the important difference |
| 15 | between apply() and funcall(). For example: |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 16 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 17 | mixed eval(object ob, string func, mixed *args) |
| 18 | { |
| 19 | return apply(#'call_other, ob, func, args); |
| 20 | } |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 21 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 22 | This will result in calling |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 23 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 24 | ob->func(args[0],args[1],...,args[sizeof(args)-1]). |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 25 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 26 | Using funcall() instead of apply() would have given us |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 27 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 28 | ob->func(args). |
| 29 | |
| 30 | |
| 31 | Of course, with the '...' operator we could also write |
| 32 | |
| 33 | mixed eval(object ob, string func, mixed *args) |
| 34 | { |
| 35 | return funcall(#'call_other, ob, func, args...); |
| 36 | } |
| 37 | |
| 38 | and achieve the same result. |
| 39 | |
| 40 | HISTORY |
| 41 | Introduced in 3.2@70 |
| 42 | LDMud 3.2.8 adds the returning of a non-closure as first |
| 43 | argument. |
| 44 | LDMud 3.3 added the '...' operator and thus made apply() in fact |
| 45 | redundant. |
| 46 | LDMud 3.3.266 added support for structs. |
| 47 | |
| 48 | SEE ALSO |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 49 | funcall(E), closures(LPC), varargs(LPC) |