1
$\begingroup$

Is there a way to skip some middle optional arguments? For example for this function:

f[{a_: 80, b_: 90}, {c_: 30, d_: 20}] := 
 Print["a=", a, ", b=", b, ", c=", c, ", d=", d]

Call something like this:

f[{(* empty or some symbols*), 3}, {4, 5}]

and get the result:

a=80, b=3, c=4, d=5

P.S. Function will have many parameters and will have complicated form, so the answer described here may be not applicable.

$\endgroup$
3
  • 2
    $\begingroup$ You can prioritize certain arguments/argument sequences using Longest. Another option is to explicitly pass Automatic or similar to the function, and then do some preprocessing inside the function. But personally, I would strongly consider just passing in all values as named options in cases like these: Then it is trivial to skip arguments, and it is very clear which arguments are assigned for any given call $\endgroup$
    – Lukas Lang
    Aug 18, 2021 at 9:39
  • $\begingroup$ Thank you, Lukas, for your response. Can you please explain in more details? You mean I need to do something like presented here? $\endgroup$
    – Tigran
    Aug 19, 2021 at 16:36
  • 1
    $\begingroup$ yes exactly, something like that. $\endgroup$
    – Lukas Lang
    Aug 19, 2021 at 18:34

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.