Search code examples
language-agnosticprogramming-languageslanguage-design

Uses for variable args syntax when shorthand lists and method signature overriding are available


For a language that has an easy shorthand syntax for lists, ex

x = [1,2,3];

And the ability to overload functions with different signatures...

Are there any uses for a variable argument syntax for functions that wouldn't be just as clearly done via lists? For example, a common use for varargs is for a printf type statement:

printf("This is %s while this is a number %d\n", "a string", 10);

However, you could just as easily and clearly do the above with:

printf("This is %s while this is a number %d\n", ["a string", 10]);

The only thing I've been able to come up with so far would be the implementation of control flow structures like if/elseif/else where you'd want to be able to pass in a variable number of elseif/else blocks. However, this could be avoided by breaking elseif into else with an if as the single command.

So, are there types of things cannot be done (cleanly/clearly) without specifically having a varargs syntax?


Solution

  • Variable args should be included even when shorthand lists can serve the same purpose since they increase intuitiveness and reduce errors.

    [This is assuming that you agree an acceptable "use" for a language feature is intuitiveness and error reduction.]

    I base this on the purely anecdotal experience of having both of these features in a proprietary game scripting language and the observed learning curve and common errors made by the users. [The users ranged from fresh out of school to industry veterans on several multi-year projects over a decade and a half in different game companies - the largest with 160 team members (20 core script users + 50 or so script real-time "command-line" users). Some were pure scripters that only knew this scripting language or a couple others and some were also expert C++ programmers - both groups seemed to have the same results on this issue.]

    [If it is relevant - the language that I am basing this on also had typed variable arguments and all types have a common base class.]

    Over the years I created a number of dual methods that were identical though one used variable length arguments and another used a list. Internally both the methods with different interfaces would boil down to the same code - they amounted to a stylistically syntactic difference only.

    In the "wild" I found the mechanism using a shorthand list with two additional characters to include meant that there are just two more characters that can be forgotten. I base this on the number of times I was called to "help out" users that were having trouble with the list based versions of methods as opposed to the variable argument methods.

    As for intuitiveness I've done some profiling/counting on methods and the number of instances of using the variable length methods vastly outweighed the list methods. Also when asked people seemed to aesthetically prefer the variable arg versions.

    However I still believe that there is great utility in having the list based methods especially when making more complex calls and meta-programming since it is handy to treat a bunch of arguments as a single unit and common list functions - intersections, unions, filtering, etc. can be used on the arguments as well.

    I'm a big believer of only adding the features that are necessary to reduce the amount that a given programmer needs to keep in their head at any one time though over the years I have found that a valid exception to this rule is sometimes warranted if a feature is easier to use or causes less errors. [An earlier scripting language I wrote was very streamlined - and elegant in my opinion - though I learned the hard way that people liked it more and had less problems with it once I evolved it to have a little more "redundancy" in the name of intuitiveness and error reduction.]

    Obviously different languages with different elements around these features and different users and domains could have different results - though for what it is worth I’m fairly confident based on how long I have made my observations.

    [Also]

    Another potential concrete difference between variable args and shorthand lists depends on whether the language can specify a difference between how arguments are passed into a variable arg aspect of a method/function interface and arguments/statements used as elements in a shorthand list.

    If the variable arg parameter can specify something above and beyond what can be specified for a shorthand list (or vice-versa) – i.e. arguments are passed by reference rather or by value, arguments are to use some sort of lazy evaluation or evaluated on call, etc. – then more could be done with one or the other syntax.

    This is really language dependant and there would be no difference if arguments used by a variable argument “group” or shorthand list elements could be “passed” in the same way.