[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[sc-users] spreading an array across multiple function arguments



Dear all,

I'm trying to write a helper function that wraps OSC message sends with variable arguments. The variable input arguments is no problem, using the `... args` syntax.

Now I'm wondering how to distribute the values of the args array across the input arguments of a function that also has variable arguments (NetAddr's sendMsg method).

This must be possible in SC lang, but I'm stumped on what to search for... I feel like I've even written functions that do this in the past...

right now my extremely ugly solution is to use a switch statement up to some number of reasonable arguments.

```

  *send {|obj, method ... args|
    var na = this.netAddr;
    switch(args.size,
      0, { na.sendMsg(obj, method) },
      1, { na.sendMsg(obj, method, args[0]) },
      2, { na.sendMsg(obj, method, args[0], args[1]) },
      3, { na.sendMsg(obj, method, args[0], args[1], args[2]) },
      4, { na.sendMsg(obj, method, args[0], args[1], args[2], args[3]) },
      5, { na.sendMsg(obj, method, args[0], args[1], args[2], args[3], args[4]) },
      6, { na.sendMsg(obj, method, args[0], args[1], args[2], args[3], args[4], args[5]) },
      7, { na.sendMsg(obj, method, args[0], args[1], args[2], args[3], args[4], args[5], args[6]) },
    );
  }

```