Behavior of Remove inside a Package

I have a notebook containing (in a Code cell, Initialization Group)

BeginPackage["aPackage"];
aTestFunction::usage = "aTestFunction usage";
Begin["Private"];
(* Remove[aTestFunction]; *)
aTestFunction[arg_] := arg + 1;
Protect[aTestFunction];
End[];
EndPackage;


and further

Needs["aPackage", NotebookFileName[]];
?aTestFunction
aTestFunction[1]
Quit[];


All that honestly earns its pay. But if I de - comment the Remove command, it produces.

and I can' t realize the reason. All things considered, Remove removes the aTestFunction symbol (provided that it exists) from the aPackage context but should not prevent it from being defined at a later time. It sounds quite different from what one can observe in the Global context: this

Context[]
a = 1;
Remove[a];
a = 2;
a + 1


returns

Global
3


.

Shortly:

Here what is going on, aTestFunction was created in aPackage context, then you've removed it and since the current context is aPackagePrivate the definition of your function is created there.

More precisely:

New symbols are created in current $Context during read time, (1) the existance of aTestFunction between BeginPackage and Begin lines makes the aTestFunction an exported symbol (that means in aPackage, because the symbol was not found and the current $Context is apackage).

After (2) Remove it's no longer there and (3) your definition is read in aPackagePrivate context. But aPackagePrivate is not going to be on $ContextPath or $Context after EndPackage[] so it is not found by ?.

I'm strongly encouraging to read a very nice answer of Szabolcs:

How symbol lookup actually works

BeginPackage["aPackage"];
aTestFunction::usage = "aTestFunction usage"; (*1*)
Begin["Private"];
Remove[aTestFunction]; (*2*)
aTestFunction[arg_] := arg + 1; (*3*)
Protect[aTestFunction];
End[];
EndPackage;


? aTestFunction


? aPackagePrivateaTestFunction

• Hello ! Thanks for your reply ! It's all clear now. I think that my oversight was in leaving behind that usage command is totally different from a simple string assignment. Apr 8 '16 at 13:59