[comp.sys.next] Variable Length Argument Lists for Methods

brian@lighthouse.com (Brian Douglas Skinner) (09/13/89)

We're trying to write a method that takes a variable number of
arguments, something like:

[error notify: "Error at line %i in file %s", __LINE__, __FILE__];

or 

[error notify: "Error: %s at line %i in file %s", "Big Bummer",
__LINE__, __FILE__];

We've settled on

- error: (char *)format, ...;

in our .h file (based on selectorRPC:paramTypes:... in Speaker.h), but
we haven't stumbled onto how to write the method.

On page 4-13 of the Objective-C Compiler User Reference Manual (in
Volume 3 of the NeXT 0.9 Technical Documentation) is written

- error:( char * ) format ; int aList ;
//                        ^
//                        might be a colon; can't tell

However, this results in a compiler error.

We've tried variations on the ANSI stdarg technique, and we've tried
messing directly with pointers, to no avail.

In our .m file, we can use

- error: (char *)format, ...
{
    char *args;
    char **scary;
    char out[255];

    scary = &format;
    scary++;
    args = *scary;

    sprintf(out, format, args);
    NXAlert(out, "OK", NULL, NULL);

    return self;
}

without getting a compiler error, but it dumps core in sprintf.

Hints and code fragments would be greatly appreciated.

Thanks,
Brian Skinner
Lighthouse Design, Ltd.
Usenet:   ...!uunet!lighthouse!brian
Internet: brian@lighthouse.com