Re: Contexts and Naming of Things
- To: mathgroup at smc.vnet.net
- Subject: [mg25730] Re: Contexts and Naming of Things
- From: "Allan Hayes" <hay at haystack.demon.co.uk>
- Date: Sat, 21 Oct 2000 14:42:44 -0400 (EDT)
- References: <8smd2e$lav@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Mike:
When your code is loaded:
BeginPackage["HSSV`cavity`"]
foo::usage = "now is the time..."
Begin["`Private`"]
bar;
nebbish;
foo[x_, y_] := 1 /; y === bar;
foo[x_, y_] := 2 /; y === nebbish;
End[]
EndPackage[]
bar and nebbish are created as HSSV`cavity`Private`bar and
HSSV`cavity`Private`nebbish.
The first rule becomes
HSSV`cavity`foo[HSSV`cavity`Private`x_, HSSV`cavity`Private`y_] :=
1 /; HSSV`cavity`Private`y === HSSV`cavity`Private`bar;
But when you enter foo[1, bar], bar is created as Global`bar (since
"HSSV`cavity`Private`" is not on the context search path. The full
expression becomes
HSSV`cavity`foo[1, Global`bar]]
Which does not match the left side of the stored rule .
We do get
foo[1, HSSV`cavity`Private`bar]
1
But the obvious way out is to rearrange the code so that on loading bar and
nebbish are created in the package context like foo: that is as
HSSV`cavity`bar and HSSV`cavity`nebbish:
Thus
Quit the kernel.
BeginPackage["HSSV`cavity`"]
foo::usage = "now is the time..."
bar;
nebbish;
Begin["`Private`"]
foo[x_, y_] := 1 /; y === bar;
foo[x_, y_] := 2 /; y === nebbish;
End[]
EndPackage
Now the stored rule is
HSSV`cavity`foo[HSSV`cavity`Private`x_, HSSV`cavity`Private`y_] :=
1 /; HSSV`cavity`Private`y === HSSV`cavity`bar;
And when you enter foo[1, bar], bar is created as HSSV`cavity`bar; the
expression becomes
foo[1, HSSV`cavity`bar]
which matches the left side of the stored rule;
and we get
foo[1, bar]
1
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
"Mike Yukish" <may106 at psu.edu> wrote in message
news:8smd2e$lav at smc.vnet.net...
> I have the following code which does not work as I had hoped. I first
> declare a function in a package...
>
> BeginPackage["HSSV`cavity`"]
> foo::usage = "now is the time..."
> Begin["`Private`"]
> bar;
> nebbish;
> foo[x_, y_] := 1 /; y === bar;
> foo[x_, y_] := 2 /; y === nebbish;
> End[]
> EndPackage[]
>
> And then try to run the following simple commands...
>
> foo[1, bar]
> foo[1, cavity`bar]
> foo[1, HSSV`cavity`bar]
>
> I had thought that the first one would have given me the correct answer,
> as it works OK when I do not embed the function definition in a package.
> The others were desperate second efforts that also failed. What am I
> missing here?
>
>
>
>
>