Re: Context confusion
- To: mathgroup at christensen.cybernetics.net
- Subject: [mg1543] Re: [mg1530] Context confusion
- From: twj (Tom Wickham-Jones)
- Date: Sat, 24 Jun 1995 02:57:34 -0400
Daryl Reece writes
> Back to my context confusion question. I think it is behaving like I
>originally thought so my question still exists. Maybe someone can read
>this example and explain why I am confused:
>
> BeginPackage["Thermal`", "Packages`Miscellaneous`Units`",
> "Packages`Miscellaneous`SIUnits`", "Global`"];
>
> Needs::nocont:
> Warning:Context Packages`Miscellaneous`Units`
> was not created when Needs was evaluated.
> Needs::nocont:
> Warning:Context Packages`Miscellaneous`SIUnits`
> was not created when Needs was evaluated.
BeginPackage will do a Needs on the optional arguments (they follow
the first). In this case Needs[ "Packages`Miscellaneous`Units`"]
is evaluated and so on. It appears that the Packages directory is on
$Path and so the Miscellaneous`Units` package is found and this
is loaded. The context defined in this package is Miscellaneous`Units`
not Packages`Miscellaneous`Units`, the context in the original Needs.
Thus a warning message tells you that you did Needs of a context and
did not create that a context, an indication that something is wrong.
The solution here is to use the proper name for the package which is
Miscellaneous`Units`. Thus the BeginPackage should look like
BeginPackage["Thermal`", "Miscellaneous`Units`",
"Miscellaneous`SIUnits`", "Global`"];
> Options[Grashof] = {Temperature->300 Kelvin}
>
> Kelvin::shdw:
> Warning: Symbol Kelvin appears in multiple contexts
> {Thermal`, Miscellaneous`SIUnits`}; definitions in
> context Thermal` may shadow or be shadowed by other
> definitions.
>"When Mathematica encounters a symbol
>in the input that you type or that is read from a file, it searches the
>current context and then all the contexts on the context path for this
>symbol." Why then does MMA create 2 versions of Kelvin when both contexts
>are on the $ContextPath?
Look at $ContextPath and $Context and you will see what is happening...
In[3]:= $ContextPath
Out[3]= {Thermal`, Packages`Miscellaneous`Units`,
> Packages`Miscellaneous`SIUnits`, Global`, System`}
In[4]:= $Context
Out[4]= Thermal`
Thus when a symbol Kelvin is created it is not found in any context
in $Context or $ContextPath. The contexts on the latter include
Packages`Miscellaneous`Units` and not Miscellaneous`Units`.
This means a new symbol Kelvin is created in $Context, Thermal`,
and this shadows the symbol Kelvin in Miscellaneous`Units`.
So the solution to these problems is to change the BeginPackage to:
BeginPackage["Thermal`", "Miscellaneous`Units`",
"Miscellaneous`SIUnits`", "Global`"];
Tom Wickham-Jones
WRI