I want to refer to the actual option values of a function f outside of f, using OptionValue. Since I would use the same reference in different function f, g, etc., each resolving to its own actual option values, I do not want to specify the function name f in the external OptionValue call.
OptionValue["opt"] is not resolved to OptionValue[f, "opt"] inside f. Can I circumvent this without explicitly stating OptionValue[f, "opt"] in the external assoc? BTW, that wont' work either, returning the default 1 instead of 2.
ClearAll[f];
Options[f] = {"opt" -> 1};
f[x_, OptionsPattern[]] := {
OptionValue["opt"], (* This is evaluated correctly *)
x["Option"], (* This is not resolved correctly *)
Evaluate[x["Option"]] (* This is not resolved correctly *)
};
assoc = <|"Option" :> OptionValue["opt"]|>
f[assoc, "opt" -> 2]
Output:
{2, OptionValue["opt"], OptionValue["opt"]}
My expected result would be {2, 1, 1}, though I understand that the special nature of OptionValue prevents the kernel to resolve OptionValue["opt"] to OptionValue[f, "opt"] at the time it is first encountered.
OptionValueis a magic head, which gets expanded to 3 - argument form before the r.h.s. of a function is evaluated. Which would mean that you can't accomplish directly what you are after - of course unless you use the 3-arg form ofOptionValuedirectly. But, what is the actual need / problem? It looks like you would like to hide one option behind some other, such that both work, but only one is exposed / documented. If that is the case, I can suggest a way which I've used in such cases. – Leonid Shifrin Jun 16 '21 at 09:18f1,f2, ..., which use roughly the same set of options, but with different values. I want to set up a global associationassocthat can be used by any of thef_i, and references to options inassocwould magically be resolved to the actual option values of thef_ithat uses it. The point is, thatassocis the one my clients should interact with (define), and not thef_i, so I want to make option-reference as trivial as possible, without introducing global symbols for all option values. Does it clarify my aim? – István Zachar Jun 16 '21 at 10:07OptionValueto define parts ofassoc(which holds more key-value pairs in reality). Moreover, I have nested options... – István Zachar Jun 16 '21 at 13:59