Why use OptionsPattern, OptionsValue ?
- To: mathgroup at smc.vnet.net
- Subject: [mg102296] Why use OptionsPattern, OptionsValue ?
- From: "Ted Ersek" <ersekt at md.metrocast.net>
- Date: Tue, 4 Aug 2009 04:29:31 -0400 (EDT)
Prior to Mathematica version 6 a good way to write a function with two
options was the following.
-----------------------------------------------------------
Options[foo] = {Opt1 -> val1, Opt2 -> val2};
foo[x_, opts___?OptionQ] := Module[{optn1, optn2},
{optn1, optn2} = {Opt1, Opt2} /. Flatten[{opts, Options[foo]}];
Blah[x, optn1, optn2]
]
----------------------------------------------------------
I am pretty sure the program above does the same thing in every version
of Mathematica (since version 3 if not earlier). However, as of
Mathematica 6 we have OptionsPattern, and OptionsValue. It seems the
main utility of OptionsPattern, and OptionsValue are to define functions
such as the one above.
One clear advantage to the old approach (see my example above) is that
you end up with code that works well across many versions of
Mathematica. What are the advantages to implementing options using
OptionsPattern, and OptionsValue?
Thanks,
Ted Ersek