MathGroup Archive 2008

[Date Index] [Thread Index] [Author Index]

Search the Archive

Passing default options to nested function call

  • To: mathgroup at
  • Subject: [mg93946] Passing default options to nested function call
  • From: Stoney Ballard <stoneyb at>
  • 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

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

  • Prev by Date: Re: Comment display style?
  • Next by Date: Re: Re: Mathematica 7: PieChart: opacity of a
  • Previous by thread: Re: tabulate an expression in arbitrary precision
  • Next by thread: RE: Passing default options to nested function call