Woud there be an elegant way to use a held option ?
For example if I need to pass by reference several variables but I don't always need all of them, it can be useful. For example if I want to append an element to a List/Association/Dataset.
I've managed to make something work but it's not very usable.
l = {};
ClearAll@ffff;
Options[ffff] = {"a" -> Hold[l]};
ffff[x_, OptionsPattern[]] := Function[Null, AppendTo[#, x], HoldFirst] @@ OptionValue["a"];
ffff[2]
l
Answer
I'd do something like this:
l = {};
ClearAll@ffff;
Options[ffff] = {"a" :> l};
ffff[x_, OptionsPattern[]] :=
OptionValue[Automatic, Automatic, "a", Function[v, AppendTo[v, x], HoldFirst]]
That is, use RuleDelayed instead of Rule to define your held option, and then use extended syntax of OptionValue.
Comments
Post a Comment