MathGroup Archive 2008

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

Search the Archive

RE: Passing default options to nested function call

  • To: mathgroup at
  • Subject: [mg93978] RE: [mg93946] Passing default options to nested function call
  • From: "David Park" <djmpark at>
  • Date: Sun, 30 Nov 2008 07:00:24 -0500 (EST)
  • References: <10132325.1227952316551.JavaMail.root@m02>

That seems all very strange to me. If you are going to have one function
nested within another and pass options, then you need distinct names for the
options. If you have the same option name 'opt2' for both functions then you
must expect some kind of restrictive behavior. For example, I don't see how
you could pass different opt2 values to the two routines.

So why not use something like this:

Options[fun2] = {opt3 -> 3};
fun2[OptionsPattern[]] := OptionValue[opt3]

fun2[opt3 -> 25]

Options[fun1] = Join[{opt1 -> 1, opt2 -> 2}, Options[fun2]];
fun1[opts : OptionsPattern[]] := {OptionValue[opt1], 
  fun2[FilterRules[{opts}, Options[fun2]]]}

fun1[opt1 -> 20, opt3 -> 25]

{1, 3}
{20, 25}

David Park
djmpark at 

From: Stoney Ballard [mailto:stoneyb at] 

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: tabulate an expression in arbitrary precision
  • Next by Date: RE: Re: v.7.0 issues
  • Previous by thread: Passing default options to nested function call
  • Next by thread: Strange behaviour of Fourier[] in Mathematica 7