Re: Options and Contexts
- To: mathgroup at yoda.physics.unc.edu (mathgroup)
- Subject: Re: Options and Contexts
- From: John Lee <lee at math.washington.edu>
- Date: Tue, 19 Apr 94 11:44:26 -0700
Daryl S. Reece <gt9397a at prism.gatech.edu> writes:
> I have a function that I want to define in its own context.
> I want to assign options to this function within its own context.
> I then want to be able to call the function from the Global
> context with options and not have to differentiate between
> the context of options assigned at the global level
> and the default values. An example will help.
> BeginPackage["Test`"];
> Tester::usage = "Tester[opts___] acts based on options.";
> Opt1::usage = "Option for Tester.";
> Begin["Test`Private`"];
> Options[Tester] = {Opt1->A};
> Tester[opts___] :=
> Module[{opt1},
> opt1 = Opt1/.{opts}/.Options[Tester];
> Print["Equality of option and A ", opt1===A];
> Print["Equality of option and B ", opt1===B];
> ]
> End[];
> EndPackage[];
> Tester[]
> Equality of option and A True
> Equality of option and B False
> Tester[Opt1->B]
> Equality of option and A False
> Equality of option and B False
> Tester[Opt1->Test`Private`B]
> Equality of option and A False
> Equality of option and B True
> These last two examples show that any options assigned at
> the global level cannot be compared within the Private contexts
> without specifically declaring their contexts. This is a problem
> since I would like to be able to call the package from any context
> and have it behave the same. Thanks ahead of time for the help.
All you have to do is to define in the "Test`" context, with usage
messages, any symbols that can legitimately be passed as options. For
example, if you add the following lines after the third line above,
A::usage = "Default value for the Opt1 option of Tester.";
B::usage = "Value for the Opt1 option of Tester.";
then you should get the behavior you want.
Jack Lee
Dept. of Mathematics
University of Washington