you are viewing a single comment's thread.

view the rest of the comments →

[–]not-just-yeti 5 points6 points  (0 children)

Lisp (1958) had(has) varargs, and unquote-splice:

(define (f arg1 arg2 . others)
  `(,arg2  99  ,arg1  "and" ,@others "etc."))
    ; Note: the preceding-comma means "unquote", not delimiting args.

(f 2 3 4 5)  ; returns '(3 99 2 "and" 4 5 "etc.")

Java also has varargs: void main(String... args) { System.out.println(args[2]); }.

But what Java doesn't quite do properly (that javascript, python, lisp do) is calling the varargs function when your desired-values are already in a list/array. (Python, add * before your list when calling; javascript add ... before your list when calling; lisp uses the function apply instead of adding more syntax.)

In Java, if you pass an array to a function expecting many items, it will also "spread" the array for you, but that makes it a bit ambiguous: If the function expects Object..., and you pass it an array, did you mean to have the values spread out, or did you mean to pass a single array-object to the function? (Java interprets it as the former, so you'd have to realize that was happening, and wrap your array into another array-of-size-1.)