MathGroup Archive 2002

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

Search the Archive

RE: named optional parameters

  • To: mathgroup at smc.vnet.net
  • Subject: [mg37921] RE: [mg37909] named optional parameters
  • From: "Wolf, Hartmut" <Hartmut.Wolf at t-systems.com>
  • Date: Tue, 19 Nov 2002 20:58:28 -0500 (EST)
  • Sender: owner-wri-mathgroup at wolfram.com

>-----Original Message-----
>From: doris.siegl at gmx.at [mailto:doris.siegl at gmx.at]
To: mathgroup at smc.vnet.net
>Sent: Tuesday, November 19, 2002 9:51 AM
>To: mathgroup at smc.vnet.net
>Subject: [mg37921] [mg37909] named optional parameters
>
>
>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}
>

Doris,

this is a common oversight and the explanation is quite simple: As you set
up your package the option names used there were:

In[15]:= Names["TestPackage`TestPackage`Private`*"]
Out[15]= {...,
   "TestPackage`TestPackage`Private`DrawCh", 
   "TestPackage`TestPackage`Private`DrawG", 
   ..."}

When calling ParamTest however, you introduced the symbols DrawCh and DrawG
of the Global context which don't match and thus the option will not be
effective.

The standard way is to export *all* the symbols of the package to be used
from an application outside with usage messages. Such including...

BeginPackage["TestPackage`TestPackage`"]

DrawCh::usage = "some option of ParamTest";
DrawG::usage = "yet another option";

...

Begin["TestPackage`TestPackage`Private`"]

...

EndPackage[]

...will make your problem disappear.

--
Hartmut Wolf



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