SYNOPSIS

mixed apply( closure cl, ...)

DESCRIPTION

Evaluates the closure cl with the following arguments. If the last argument is an array or struct, it will be flattened: ie. the array/struct itself will be removed and its contents added to the argument list of cl.

If cl is not a closure, it will simply be returned (and all other arguments are ignored).

USAGE

The flattening of the last argument is the important difference between apply(E) and funcall(E). For example:

mixed eval(object ob, string func, mixed *args)
{
  return apply(#'call_other, ob, func, args);
}

This will result in calling:

ob->func(args[0],args[1],...,args[sizeof(args)-1]).

Using funcall(E) instead of apply(E) would have given us:

ob->func(args).

Of course, with the ‘...’ operator we could also write:

mixed eval(object ob, string func, mixed *args)
{
  return funcall(#'call_other, ob, func, args...);
}

and achieve the same result.

HISTORY

  • introduced (3.2@70)
  • changed (3.2.8) – adds the returning of a non-closure as first argument.
  • changed (3.3) – added the ‘...’ operator and thus made apply(E) in fact redundant.
  • changed (3.3.266) – added support for structs.