1
$\begingroup$

Suppose a function has multiple argument structure like follows:

f[a_,{b_,c___}]:=... Echo[{ArgumentSequence[]},"called with:"] ...;
f[{a_,b_},c___]:=... Echo[{ArgumentSequence[]},"called with:"] ...;
f[{{a_},b_},c___]:=... Echo[{ArgumentSequence[]},"called with:"] ...;
...

Is there a function ArgumentSequence that would get the sequence of arguments by which the function was called instead of hardwiring it in every definition?

So for f[{10,11},12,13], ArgumentSequence[] when called inside the body would evaluate to Sequence[{10,11},12,13]. Note if the function f had Hold attribute on an argument like HoldFirst then for f[{10,a},b,13] where a=11 and b=12, ArgumentSequence[] gives Sequence[HoldForm[{10,a}],12,13].

Basically this is just like the SlotSequence for pure functions, ...Echo[{#0,##},"calling information:"]...&[...].

Also #0 evaluate to the full definition of the pure function since it doesn't have a name but for the case of functions defined using DownValues is there a similar analog which instead of returning the DownValue information returns the name of the function f.

Also is there a function DownValueMatch which when called gives the rule which will be matched.

So for example, if I call DownValueMatch[f[{10,11},12,13]] will give

{HoldPattern[f[{a_, b_}, c___]] :>  ... Echo[ArgumentSequence[],"called with:"] ...}
$\endgroup$
  • 4
    $\begingroup$ maybe foo[p : PatternSequence[{a_, b_}, c_]] := (Echo[{p}];...)? $\endgroup$ – kglr Apr 18 at 22:16
  • 1
    $\begingroup$ Why not just have a wrapper function g[args__] := Module[(* stuff *), Echo[{args}, "called with:"]; f[args]], and leave the special cases to f's definitions? $\endgroup$ – J. M.'s discontentment Apr 19 at 1:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.