varargs

Passing on named variable arguments in python

Say I have the following 2 methods: def methodA(arg, **kwargs): pass def methodB(arg, *args, **kwargs): pass In methodA I wish to call methodB, passing on the kwargs. However, it seems if I simply do: def methodA(arg, **kwargs): methodB("argvalue", kwargs) The second argument will be passed on as positional rather than...

Is it possible to explode an array so that its elements can be passed to a method with the params keyword?

Take this non-compiling code for instance: public string GetPath(string basefolder, string[] extraFolders) { string version = Versioner.GetBuildAndDotNetVersions(); string callingModule = StackCrawler.GetCallingModuleName(); return AppendFolders(basefolder, version, callingModule, extraFolders); } private string AppendFolder...

C/C++: Passing variable number of arguments around

Say I have a C function which takes a variable number of arguments: How can I call another function which expects a variable number of arguments from inside of it, passing all the arguments that got into the first function? Example: void format_string(char *fmt, ...); void debug_print(int dbg_lvl, char *fmt, ...) { format_string(f...

Are there gotchas using varargs with reference parameters

I have this piece of code (summarized)... AnsiString working(AnsiString format,...) { va_list argptr; AnsiString buff; va_start(argptr, format); buff.vprintf(format.c_str(), argptr); va_end(argptr); return buff; } And, on the basis that pass by reference is preferred where possible, I changed it thusly. Ansi...

Am I missing something, or do varargs break Arrays.asList?

private void activateRecords(long[] stuff) { ... api.activateRecords(Arrays.asList(specIdsToActivate)); } Shouldn't this call to Arrays.asList return a list of Longs? Instead it is returning a List<long[]> public static <T> List<T> asList(T... a) The method signature is consistent with the results, the varargs throws th...

How can a function with 'varargs' retrieve the contents of the stack?

Normally, in Delphi one would declare a function with a variable number of arguments using the 'array of const' method. However, for compatibility with code written in C, there's an much-unknown 'varargs' directive that can be added to a function declaration (I learned this while reading Rudy's excellent 'Pitfalls of convering' document...

difference fn(String... args) vs fn(String[] args)

Whats this syntax useful for : **function(String... args)** Is this same as writing **function(String[] args)** with difference only while invoking this method or is there any other feature involved with it ? ...

How to pass varargs into a method as an expanded list?

Given the following Flash method: function sendToJava(name:String, ... args) { ExternalInterface.call("sendCommand", name, args); } How do I ensure that ExternalInterface.call() interprets args in its expanded form? Right now, if I pass a list into "args", that list gets interpreted as a single argument of type "Object[]" by Externa...

recursively concatenating a javascript functions arguments

I came across a javascript puzzle asking: Write a one-line piece of JavaScript code that concatenates all strings passed into a function: function concatenate(/*any number of strings*/) { var string = /*your one line here*/ return string; } @ meebo Seeing that the function arguments are represented as an index...

Wrapping a Vararg Method in ActionScipt

I have a vararg method that I'd like to act as a proxy for another vararg method, but I'm not sure how to do it. Here's the basic code: class MyClass { public function a(...args:*):* { // other code b(args); // other code } public function b(...args:*):* { // do stuff with args } } I'm porting th...

Java "params" in method signature?

In c# if you want a method to have an indeterminate number of parameters you can make the final parameter in the method signature a "params" which to the method looks like an array but allows anyone using the method to put in as many parameters of that type as the want. I'm fairly sure java supports similar behaviour, but I cant find ou...

Is there a varargs null check function in Java or Apache Commons?

I've got four variables and I want to check if any one of them is null. I can do if (null == a || null == b || null == c || null == d) { ... } but what I really want is if (anyNull(a, b, c, d)) { ... } but I don't want to write it myself. Does this function exist in any common Java library? I checked Commons Lang and didn't...

When do you use varargs in Java?

I'm afraid of varargs. I don't know what to use them for. Plus, it feels dangerous to let people pass as many arguments as they want. What's an example of a context that would be a good place to use them? ...

Can you PInvoke multibyte ANSI to a varargs? What am I doing wrong?

** MAJOR UPDATE ** I made a minor mistake but I'm still curious about exactly what is happening. The function I am calling is actually "fooV", a function with this signature: foo(const char *, const char *, EnumType, va_list) This clears up the AccessViolationExceptions I was getting, but doesn't explain why params parameters work fo...

Calling swprint from a separate lib fails

Hi all I am facing a strange problem. I am using sprintf or swprintf according to the build defines with or without unicode. I have wrapped these functions in my own function like this: int mysprintf( MCHAR* str,size_t size, const MCHAR* format, ... ) { #ifdef MYUNICODE return swprintf( str, size, format); #else return snprintf...

Should I cast a CString passed to Format/printf (and varargs in general)?

I recently took in a small MCF C++ application, which is obviously in a working state. To get started I'm running PC-Lint over the code, and lint is complaining that CStringT's are being passed to Format. Opinion on the internet seems to be divided. Some say that CSting is designed to handle this use case without error, but others (an...

In Java, why is the call foo() not ambigious given 2 varags methods foo(int... ints) and foo(Object... objects)?

If I declare just the 2 varargs methods as follows: public void foo(String... strings) { System.out.println("Foo with Strings"); } and public void foo(int... ints) { System.out.println("Foo with ints"); } and then have the code: foo(); this is a compiler error due to the ambiguity as expected. However if I have just the...

Log a message with a variable number of printf-style arguments

I'm trying to implement a C# method that can log a message with a format string and a variable number of printf-style arguments. Ideally, it would work identically to the C function I pasted below, although idiomatic to C# of course. static void LogMessage(const char * iFormat, ...) { va_list argp; FILE * fp; fp = fopen("log.txt...

Converting the "arguments" object to an array in javascript

As a trivia question, I'm trying to write a javascript function that returns its variable number of arguments in sorted (normal lexicographic) order. Having never dealt with javascript before, I came across the "arguments" object, which seems to work somewhat like an array but without having the standard array functions -- therefore I s...

Using varargs from Scala

I'm tearing my hair out trying to figure out how to do the following: def foo(msf: String, o: Any, os: Any*) = { println( String.format(msf, o :: List(os:_*)) ) } There's a reason why I have to declare the method with an o and an os Seq separately. Basically, I end up with the format method called with a single object parameter (o...