Re: named optional parameters
- To: mathgroup at smc.vnet.net
- Subject: [mg37934] Re: named optional parameters
- From: Rainer Gruber <rainer.gruber at gmx.at>
- Date: Wed, 20 Nov 2002 09:09:04 -0500 (EST)
- Organization: Johannes Kepler Universitaet Linz
- References: <arcu57$fjg$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Doris S. wrote: > 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} > I think the problem is the 'Context' of your options In the Definition of 'ParamTest' you use e. g. the symbol TestPackage`TestPackage`Private`DrawG but if you evalaute ParamTest[DrawG -> False] you use the symbol Global`DrawG ParamTest1 works, beacause its options are appears in the Global context.So the solution for your problem is to create Global`DrawG first, eg. with Unprotect[ParamTest, DrawG,...]; ClearAll[ParamTest, DrawG,...]; or DrawG::usage="..." after BeginPackage["TestPackage`TestPackage`"] hope that helps, Rainer