Re: suppressing initialization
- To: mathgroup at smc.vnet.net
- Subject: [mg4606] Re: [mg4523] suppressing initialization
- From: Allan Hayes <hay at haystack.demon.co.uk>
- Date: Wed, 21 Aug 1996 03:25:24 -0400
- Sender: owner-wri-mathgroup at wolfram.com
beatrous+ at pitt.edu (Frank Beatrous)
[mg4523] suppressing initialization
writes
>>>>>>>
I would like to find a clean way to suppress the loading of a package
that's defined in initialization cells of notebooks when the
package is already loaded at the time the notebook is initialized.
The "obvious" thing to do is to put code something like this in the
initialization:
If[!MemberQ[$Packages,"mystuff`"],
BeginPackage["mystuff`"];
x::usage="This should go in the context mystuff`.";
Begin["`Private`"];
x = a;
y = b;
End[];
EndPackage[];
]
Unfortunately, the symbols x and y both land in the Global`
context instead of mystuff` and mystuff`Private` where they
belong. Evidently, the code inside of If[] uses its own local
version of $Context, rather than the global one that's
manipulated by BeginPackage[] and Begin[].
<<<<<<<<
Frank,
The reason for x and other variables being created in context
"Global`" is that creation of variables is done before evaluation
(while the context is "Global`") -- nothing to do with If.
So we need to change contexts before reading expressions.
This suggests the following form.
If[mystuff`Private`load = !MemberQ[$Packages,"mystuff`"],
BeginPackage["mystuff`"]
];
If[mystuff`Private`load,
x::usage="This should go in the context mystuff`.";
Begin["`Private`"]
];
If[load,
x = a;
y = b;
End[];
EndPackage[]
];
Remove[mystuff`Private`load]
The use of mystuff`Private`load fits in with the package.
Remove[mystuff`Private`load] removes the evidence -- it's not
really needed since there is other scaffolding debris left around if
the package is loaded, for example mystuff`Private`y.
Allan Hayes
hay at haystack.demon.co.uk
==== [MESSAGE SEPARATOR] ====