[DGD] Re: varargs int ...
Erwin Harte
harte at is-here.com
Sat Feb 21 16:36:23 CET 2004
On Sat, Feb 21, 2004 at 03:04:16AM -0500, Michael McKiel wrote:
> Is it possible to test if a varargs int parameter was passed to a given
> function or not?
> I was playing around with a strstr() function, and seems I want to make it
> work differently if the 2nd int variable wasn't passed ie:
> string strstr(string str, int start, varargs int end) {
>
> if I don't pass it 'end' then the function should assume end = strlen(str)-1
> if I pass it end == 0, then it should return a nil string, since it would be
> a zero length string.
> But it seems all I can test for is !end
> and that is true if I don't pass an end variable, and IF I pass it 0...
>
> So what am I missing?
The only way I can see to be 100% sure what parameters were provided
or not, is by using the ellipsis:
string strstr(string str, int start, varargs mixed extra...) {
int end;
if (sizeof(extra) == 0) {
/* No extra parameters provided. */
} else if (sizeof(extra) == 1 && typeof(extra[0]) == T_INT) {
/* Extra parameter was provided and was an integer. */
end = extra[0];
} else {
error("bad argument 3 to strstr");
}
...
}
The next best thing is:
string strstr(string str, int start, varargs mixed end) {
if (end == nil) {
/* Varargs parameter omitted or 'nil' value used. */
} else if (typeof(end) == T_INT) {
/* Extra parameter provided and was an integer. */
} else {
error("bad argument 3 to strstr");
}
...
}
But then you can't distinguish between strstr("foo", 1, nil) and
strstr("foo", 1).
If you don't care about that distinction, then by all means go ahead
and use that approach. ;-)
Hope that helps,
Erwin.
--
Erwin Harte <harte at is-here.com>
_________________________________________________________________
List config page: http://list.imaginary.com/mailman/listinfo/dgd
More information about the DGD
mailing list