Re: Re: shadow-proofing a package
- To: mathgroup at smc.vnet.net
- Subject: [mg42744] Re: [mg42724] Re: [mg42712] shadow-proofing a package
- From: Selwyn Hollis <selwynh at earthlink.net>
- Date: Thu, 24 Jul 2003 04:10:46 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
Here's a better solution. It doesn't remove anything that has a value,
but does prevent the shadowing that happens when the user inadvertently
enters a package function prior to loading the package.
Consider the following function:
valuelessglobals[nameslist:{_String..}] :=
With[{exprs = ToExpression/@
Intersection[nameslist, ToString/@ToExpression/@nameslist]},
Complement[exprs,
Select[exprs, {UpValues[#],DownValues[#]} =!= {{},{}}& ]]]
and suppose that
mypackagenames = {"a", "b", "c", "f", "x"};
Let's say we enter
a = 1; f[x_] := x; c[something];
Then
valuelessglobals[mypackagenames]
{b, c, x}
and
Remove@@(valuelessglobals[mypackagenames])
removes the problem symbol c.
-----
Selwyn Hollis
http://www.math.armstrong.edu/faculty/hollis
On Wednesday, July 23, 2003, at 07:08 AM, Andrzej Kozlowski wrote:
> As others have pointed out, a properly written program does not erase
> or remove definitions made by the user without first saving them
> somewhere else or at least giving the user a chance to do so. However,
> you can modify your package so that it first "moves" all such symbols,
> together with their DownValues, UpValues etc, to a newly created
> context (which should be different from any existing one).
>
> Andrzej Kozlowski
> Yokohama, Japan
> http://www.mimuw.edu.pl/~akoz/
> http://platon.c.u-tokyo.ac.jp/andrzej/
>
>
>
> On Wednesday, July 23, 2003, at 06:25 AM, Dr Bob wrote:
>
>> That's essentially the method used in Notation.m
>>
>> Or here's my own utility package, featuring the same method:
>>
>> BeginPackage["DrBob`Utility`"]
>>
>> Unprotect[Evaluate[$Context<>"*"]];
>> ClearAll[Evaluate[$Context<>"*"]];
>>
>> DrBob`Utility::gshadw="The symbol '`1`' has been used in the global
>> context. \
>> The DrBob`Utility` package needs the full use of the symbol '`1`' and
>> has \
>> therefore removed this symbol from the global context.";
>>
>> Begin["`Private`"]
>>
>> publicFunctions=
>>
>> Map[StringJoin["Global`",#1]&,{"animate","browse","divvy","divvySort",
>> "self","subFunction","test","testOption","testMax
>> ","testFst","zeroQ"}];
>> overideNames=Intersection[Names["Global`*"],publicFunctions];
>> If[overideNames=!={},((Message[DrBob`Utility::gshadw,#1]&)/
>> @(StringDrop[#1,
>> 7]&)/@overideNames;
>> Unprotect/@overideNames;
>> ClearAll/@overideNames;
>> Remove/@overideNames;
>> Null)];
>>
>> End[]
>>
>> ... usage statements here ...
>>
>> Begin["`Private`"]
>>
>> ... function definitions ...
>>
>> End[]
>>
>> Protect[Evaluate[$Context<>"*"]];
>>
>> EndPackage[];
>>
>> Bobby
>>
>> On Tue, 22 Jul 2003 04:40:46 -0400 (EDT), Selwyn Hollis
>> <selwynh at earthlink.net> wrote:
>>
>>> The following occurred to me as a way to prevent the shadowing
>>> problem:
>>>
>>> Before BeginPackage["blah`blahblah`"] put
>>>
>>> Remove@@( StringJoin["Global`",#]&/@
>>> Intersection[Names["Global`*"],
>>> {"name1", "name2", ... for all symbols defined in the package}] )
>>>
>>> This seems to work well. But surely there must be some downside to
>>> it, or
>>> else it would already be the recommended way of doing things. What
>>> am I
>>> overlooking here?
>>>
>>> -----
>>> Selwyn Hollis
>>> http://www.math.armstrong.edu/faculty/hollis
>>>
>>>
>>
>>
>>
>> --
>> majort at cox-internet.com
>> Bobby R. Treat
>>
>>
>>
>
>