1
$\begingroup$

This question already has an answer here:

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!

$\endgroup$

marked as duplicate by Mr.Wizard Feb 8 '14 at 13:00

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • $\begingroup$ Please also see: (28524), (18004), (7502) $\endgroup$ – Mr.Wizard Feb 8 '14 at 13:05
  • $\begingroup$ If after reading the marked duplicate and the links above you are still having trouble please let me know. $\endgroup$ – Mr.Wizard Feb 8 '14 at 13:08
  • $\begingroup$ Thank you, that was an extremely fast and helpful resonse. Thanks for pointing to the different solutions. For the sake of completeness I will add the working package below in case someone stumbles over this post. $\endgroup$ – Niels Feb 8 '14 at 13:32
  • $\begingroup$ Niels, I converted your comment to an edit of your question so that the code would be readable. Two remarks: you can effectively "declare" these values without using ::usage; simply listing them ({a, b, c}) before Begin["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. $\endgroup$ – Mr.Wizard Feb 8 '14 at 13:39
  • $\begingroup$ It seems we both edited the question. Sorry. By the way, you can format code by selecting it, then pressing Ctrl + K or clicking the { } icon at the top of the editor. $\endgroup$ – Mr.Wizard Feb 8 '14 at 13:43