Next: , Previous: , Up: Methods   [Index]


Variable method arguments

Variable Method Arguments

Ctalk supports variable arguments lists for methods that follow the stdarg.h format for argument lists, where the argument assigned to the last named parameter determines the number and type of the following arguments. (The manual page stdarg (3) has more details.)


  String instanceMethod writeFormat (char *fmt, ...)

In addition, Ctalk supports argument lists with no named parameters. To implement this, Ctalk interprets an ellipsis as a variable argument list, as usual. It is then up to the method to interpret the arguments as they appear on Ctalk’s argument stack.

For this task, the __ctalkNArgs library function returns the number of arguments that the run-time libraries place on the stack. The method can then interpret these arguments as necessary.

Here is the slightly abbreviated code for the List : = method, which should help illustrate this. See List.


List instanceMethod = initEQ (...) {
  int n, i;
  OBJECT *arg;

  self delete;         /* Start with an empty List. */
  n = __ctalkNArgs ();

  for (i = (n - 1); i >= 0; --i) {
    arg = __ctalk_arg_internal (i);
    self push arg;
  }
}