On Thu, Mar 14, 2024 at 05:57:21PM +0000, Andy Valencia via Digitalmars-d-learn 
wrote:
> Can somebody give me a starting point for understanding varadic
> functions?  I know that we can declare them
> 
>   int[] args...
> 
> and pick through whatever the caller provided.  But if the caller
> wants to pass two int's and a _string_?  That declaration won't permit
> it.
> 
> I've looked into the formatter, and also the varargs implementation.
> But it's a bit of a trip through a funhouse full of mirrors.  Can
> somebody describe the basic language approach to non-uniform varargs,
> and then I can take it the rest of the way reading the library.
[...]

The best way to do multi-type varags in D is to use templates:

        import std;
        void myFunc(Args...)(Args args) {
                foreach (i, arg; args) {
                        writefln("parameter %d is a %s with value %s",
                                i, typeof(arg), arg);
                }
        }

        void main() {
                myFunc(123, 3.14159, "blah blah", [ 1, 2, 3 ], new Object());
        }

D also supports C-style varags (without templates), but it's not
recommended because it's not type-safe. You can find the description in
the language docs.


T

-- 
"Maybe" is a strange word.  When mom or dad says it it means "yes", but when my 
big brothers say it it means "no"! -- PJ jr.

Reply via email to