EDIT: After helpful comment from @Mr.Wizard I added the working implementation below for future reference.
I wrote a module that does something with variables provided in form of a rule. It works nicely. A simplified version:
ruleAdd[assList_] := Module[{},
a + b + c /. assList
]
This can be called by
ruleAdd[{a -> 1, b -> 3, c -> 5}]
And it works nicely.
Now I would like to add this module into a package. However, from this point on I do get stuck.
The implementation that gets closest to working is the following module:
BeginPackage["MCmoduleTemplate`"]
MCfun::usage = "description"
MCfun[assList_]:=Module[
{a,b,c},
a+b+c/.assList
]
EndPackage[]
This I can run from the notebook level with the following lines:
a =.; b =.; c =.;
SetDirectory[NotebookDirectory[]];
<< MCmoduleTemplate`
MCfun[{a -> 1, b -> 3, c -> 5}]
However, a, b and c are now highlighted in red and weird behavior occurs. Is there a good way, also including the Begin["Private`"] part in the Package to properly do this?
As a workaround I am now just passing the values as parameters of the module which of course works, but I'd prefer to be able to pass rules to my modules in the long run to add flexibility.
Thanks in advance.
Please find my favourite working implementation. Please have a look at the links by Mr.Wizard for alternative solutions. Thanks again, Mr.Wizard. Niels
BeginPackage["MCrulepackage`"]
MCfun::usage = "description"
a::usage=""
b::usage=""
c::usage=""
Begin["Private`"]
MCfun[assList_]:=Module[
{(* make sure a,b and c are NOT here. *)},
a+b+c/.assList
]
End[] (* End Private` context *)
EndPackage[]
BeginPackage["MCrulepackage`"] MCfun::usage = "description" a::usage="" b::usage="" c::usage=""
Begin["Private`"]
MCfun[assList_]:=Module[ {(* make sure a,b and c are NOT here. )}, a+b+c/.assList ] End[] ( End Private` context *) EndPackage[]
-- This can now be called from the notebook as SetDirectory[NotebookDirectory[]]; << MCrulepackage` MCfun[{a -> 1, b -> 2, c -> 3}]
and works nicely.
EDIT: This is the working solution based on the links provided by @Mr.Wizard. Thanks again!
::usage; simply listing them ({a, b, c}) beforeBegin["Private`"]will cause them to be created in the `Global`` context. However, if you read all the links above you will see that experienced users recommend using Global symbols in a package like this, in most circumstances. – Mr.Wizard Feb 08 '14 at 13:39{ }icon at the top of the editor. – Mr.Wizard Feb 08 '14 at 13:43