Passing default options to nested function call
- To: mathgroup at smc.vnet.net
- Subject: [mg93946] Passing default options to nested function call
- From: Stoney Ballard <stoneyb at gmail.com>
- Date: Sat, 29 Nov 2008 04:31:32 -0500 (EST)
In the V7 tutorial "SettingUpFunctionsWithOptionalArguments", it says
to use FilterRules to create options for a called function from the
options for the outer function. An example:
Options[fun1] = {opt1 -> 1, opt2 -> 2};
Options[fun2] = {opt2 -> 3};
fun2[opts : OptionsPattern[]] := OptionValue[opt2]
fun1[opts : OptionsPattern[]] :=
{OptionValue[opt1], fun2[FilterRules[{opts}, Options[fun2]]]}
This, however, does not pass fun1's default value for opt2 into fun2.
>From what I've seen, options are applied using ReplaceAll, in the
order that they're supplied. This suggests that I can pass the default
in like this:
fun1[opts : OptionsPattern[]] :=
{OptionValue[opt1], fun2[FilterRules[{opts, Options[fun1]}, Options
[fun2]]]}
so that a call
fun1[opt2 -> 4]
will call fun2 with options list {opt2 -> 4, opt2 -> 2}
This is simple, but relies on the first occurrence in the options list
to be the only one applied. Is that ever a problem, esp. with system
functions?