MathGroup Archive 2002

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

Search the Archive

RE: named optional parameters

  • To: mathgroup at smc.vnet.net
  • Subject: [mg37922] RE: [mg37909] named optional parameters
  • From: "David Park" <djmp at earthlink.net>
  • Date: Tue, 19 Nov 2002 20:58:30 -0500 (EST)
  • Sender: owner-wri-mathgroup at wolfram.com

Doris,

You do not quite give us enough information, but I think this is what is
happening. Let's say that in your notebook you have:

Options[f]={parameter1 -> x}

Then everything works. But when you put the statement into the package,
variables go into the package context. Let's assume that you have exported
"parameter1". But the x will still be in the package context when you
intended it to be in the Global context. So just change the statement to:

Options[f]={parameter1 -> Global`x}

This is a common and easy trap to fall into. The problem can occur anywhere
you are intending to use Global symbols in a package. Also names like Wedge,
CircleTimes etc., which one might expect to be in the System context are
actually in the Global context and need to be treated similarly to x above.

David Park
djmp at earthlink.net
http://home.earthlink.net/~djmp/


From: Doris S. [mailto:doris.siegl at gmx.at]
To: mathgroup 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: Two starting programming questions re alternating groups and EulerPhi LCM numbers
  • Next by Date: RE: named optional parameters
  • Previous by thread: named optional parameters
  • Next by thread: RE: named optional parameters