How do you set attributes on SubValues?
Your question really is about how to make attributes of f
affect also the evaluation of other groups of elements, like y
and z
in f[x___][y___][z___]
. To my knowledge, you can not do it other than using tricks like returning a pure function and the like.
This is because, the only tool you have to intercept the stages of evaluation sequence when y
and z
are evaluated, is the fact the heads are evaluated first. So, anything you can do to divert the evaluation from its standard form (regarding y
and z
), must be related to evaluation of f[x]
, in particular substituting it by something like a pure function. Once you pass that stage of head evaluation, you have no more control of how y
and z
will be evaluated, as far as I know.
Generally, I see only a few possibilities to imitate this:
- return a pure function with relevant attributes (as discussed in the linked answer)
- return an auxiliary symbol with relevant attributes (similar to the first route)
- play with evaluation stack. An example of this last possibility can be found in my answer here
Here is another example with Stack
, closer to those used in the question:
ClearAll[f];
f :=
With[{stack = Stack[_]},
With[{fcallArgs =
Cases[stack, HoldForm[f[x_][y_]] :>
{ToString[Unevaluated[x]], ToString[Unevaluated[y]]}]},
(First@fcallArgs &) & /; fcallArgs =!= {}]];
And:
In[34]:= f[1 + 2][3 + 4] // InputForm
Out[34]//InputForm= {"1 + 2", "3 + 4"}
Perhaps, there are other ways I am not aware of. The general conclusion I made for myself from considering cases like this is that the extent to which one can manipulate evaluation sequence is large but limited, and once you run into a limitation like this, it is best to reconsider the design and find some other approach to the problem, since things will quickly get quite complex and go out of control.
This is an old discussion but is about an issue that resurfaces every now and then. One of the best (for a given sense of elegance) answers is the one posted on Stack Overflow
ClearAll[f]
SetAttributes[f, HoldAllComplete]
f[a_, b_, c_] := {
ToString@Unevaluated@a,
ToString@Unevaluated@b,
ToString@Unevaluated@c
}
f[a__] := Function[x, f[a, x], HoldAll]
However it contains some extra code to handle the case f[a][b, c]
, and this because the definition with Function
can only handle one argument.
There is a little undocumented feature though, that lets one define attributes for variadic functions. The last definition have to be replaced by
f[a__] := Function[Null, f[a, ##], HoldAll]
This leads to the desired result in all cases
f[1 + 1, 2 + 2, 6 + 1]
f[1 + 1, 2 + 2][6 + 1]
f[1 + 1][2 + 2, 6 + 1]
f[1 + 1][2 + 2][6 + 1]
(*
==> {"1 + 1", "2 + 2", "6 + 1"}
==> {"1 + 1", "2 + 2", "6 + 1"}
==> {"1 + 1", "2 + 2", "6 + 1"}
==> {"1 + 1", "2 + 2", "6 + 1"}
*)