MathGroup Archive 2002

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

Search the Archive

Re: named optional parameters

  • To: mathgroup at smc.vnet.net
  • Subject: [mg37930] Re: named optional parameters
  • From: "Allan Hayes" <hay at haystack.demon.co.uk>
  • Date: Tue, 19 Nov 2002 20:59:28 -0500 (EST)
  • References: <arcu57$fjg$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

The problem that you found is caused by the option names DrawCh and DrawG
being created in the context
"TestPackage`TestPackage`Private`" instead of the context
"TestPackage`TestPackage`".
At PACKAGE REWRITE below I correct this and make some other changes that you
may find useful.

What happens in your version is that the first step in the evaluation of

        ParamTest[DrawCh -> False]

is, since the context "TestPackage`TestPackage`Private`" is not searched,
that it is contextualized to

    TestPackage`TestPackage`ParamTest[System`Rule[Global`DrawCh,
System`False]]

Next, in the contextualized form of your definition we get,

TestPackage`TestPackage`Private`DrawCh/.{System`Rule[Global`DrawCh,
System`False]]}/.
System`Options[TestPackage`TestPackage`ParamTest]

and the rule System`Rule[Global`DrawCh, System`False]] has no effect on
TestPackage`TestPackage`Private`DrawCh.
This leaves the defaults options to act.


PACKAGE REWRITE

(*****************************TestPackage.m:***************************)
BeginPackage["TestPackage`TestPackage`"]

Unprotect["`*"];   (*unprotect all symbols in context
"TestPackage`TestPackage`"*)

(* create "exported" symbols in the context context
"TestPackage`TestPackage`"* at the same time as assigning usage messages to
them*)

ParamTest::usage = "usage message for ParamTest";
DrawCh::usage = "usage message for DrawCh";
DrawG::usage = "usage message for DrawG";


Begin["`Private`"]

(**********************************************************************)

Options[ParamTest]={DrawCh->True,DrawG->True};

ParamTest[opts___?OptionQ]:= (*test that input is a sequence or nested list
of rules*)
Block[{dc,dg},
dc=DrawCh/.{opts}/.Options[ParamTest];
dg=DrawG/.{opts}/.Options[ParamTest];
Return[{dc,dg}];
];

(**********************************************************************)
End[]
Protect["`*"]; (*Protect all symbols in context "TestPackage`TestPackage`"*)
EndPackage[]



The code worked within a notebook since no localizing contexts were used.

You will find a discussion of contexts in Help>Help Browser>Add-ons> Working
with Add-ons.


Allan

---------------------
Allan Hayes
Mathematica Training and Consulting
Leicester UK
www.haystack.demon.co.uk
hay at haystack.demon.co.uk
Voice: +44 (0)116 271 4198
Fax: +44 (0)870 164 0565


"Doris S." <doris.siegl at gmx.at> wrote in message
news:arcu57$fjg$1 at smc.vnet.net...
> Hello NG!
>
> I´ve got a problem using named optional parameters for a function in a
> package:
>
> Like described in chapter 2.3.10 of the Mathematica Help I created a
> list of default values for my parameters "DrawCh" and "DrawG". Then I
> wrote a function with optional parameters opts___ and evaluated them
> using the list. When I put all this (my function "ParamTest" and de
> Options for "ParamTest") into a Package and call the function from a
> notebook the given parameters are not evaluated correctly. The
> parameters just have the (default) values given in the Options list
> and don´t change as I defined them when calling the function.
> The source code of the function, nevertheless, seems to be correct,
> because putting the function outside a package and using it there
> works correctly.
>
> So do I have to define something else in the package so I can use
> named optional parameters?
>
> Additional Information:
> OS: Win2000
> Version: Mathematica 4.0
>
> I appreciate any help!
>
> Doris S.
>
>
>
> The source:
>
> (*****************************TestPackage.m:***************************)
> BeginPackage["TestPackage`TestPackage`"]
>
>
> Unprotect[ParamTest];
> Begin["TestPackage`TestPackage`Private`"]
>
> (**********************************************************************)
>
> Options[ParamTest]={DrawCh->True,DrawG->True};
>
> ParamTest[opts___]:=Block[{dc,dg},
> dc=DrawCh/.{opts}/.Options[ParamTest];
> dg=DrawG/.{opts}/.Options[ParamTest];
> Return[{dc,dg}];
> ];
>
> (**********************************************************************)
> End[]
> Protect[ParamTest];
> EndPackage[]
>
> (************* Using the function in a notebook
> ***********************)
> In[1]:= << TestPackage`TestPackage`
>
> In[2]:=  Names["TestPackage`TestPackage`*"]
>
> Out[2]= {"ParamTest"}
>
> In[3]:= ParamTest[] (*default values*)
>
> Out[3]= {True, True}
>
> In[4]:= ParamTest[DrawCh -> False] (*result should be {False,True}*)
>
> Out[4]= {True, True}
>
> In[5]:= ParamTest[DrawG -> False] (*result should be {True,False}*)
>
> Out[5]= {True, True}
>
>
> (*putting the function code of the package in a notebook and
> evaluating it gives the correct result*)
>
> In[6]:=
> Options[ParamTest1] = {DrawCh -> True, DrawG -> True};
> ParamTest1[opts___] :=
>     Block[{dc, dg}, dc = DrawCh /. {opts} /. Options[ParamTest1];
>       dg = DrawG /. {opts} /. Options[ParamTest1];
>       Return[{dc, dg}];];
>
> In[8]:= ParamTest1[DrawG -> False]
>
> Out[8]= {True, False}
>




  • Prev by Date: RE: named optional parameters
  • Next by Date: Problem with ND
  • Previous by thread: RE: named optional parameters
  • Next by thread: Re: named optional parameters