I'm experimenting with options. In my first tests, I wrote something like that to capture the options given to a function:
f[opts___Rule]:= {opts}
I later descovered OptionsPattern
:
f[opts:OptionsPattern[]]:= {opts}
Both seems to do the same thing and will produce the same result:
f[]
f[a->1]
f[a->1,b->2]
{} {a->1} {a->1,b->2}
What are the pro and cons of using opts:OptionsValues
instead of opts___Rule
to capture the options given to a function?
OptionsPattern
also acceptsRuleDelayed
and (nested) lists of rules. Furthermore it is clearer in intent and has very useful extra functionality when combined withOptionValue
$\endgroup$OptionsPattern[]
/OptionValue[]
combo is quite powerful compared to the old method, but the first approach can be made more general:f[opts___?OptionQ]:= {opts}
. $\endgroup$