I'm using IntelliJ Idea with a Mathematica plugin to develop Mathematica application. I keep IntelliJ and Mathematica opened at the same time. I would like to change something in the IDE, evaluate notebook in the Mathematica and see updated result, but Mathematica fails to see changes in my code.
My application consists of several subpackages, and Kernel/init.m
looks like
Get["MyPackage`A`"]
Get["MyPackage`B`"]
...
Notebook is something like
Quit[]
Get["MyPackage`"]
SomeFunctionFromMyPackage[]
And I've also tried
Needs["Utilities`CleanSlate`"] (* inside initialization cell *)
CleanSlate[]
Get["MyPackage`"]
SomeFunctionFromMyPackage[]
Let's say I changed SomeFunctionFromMyPackage
, saved .m
file` hit "Evaluate notebook" and hoping to see result changing, but this doesn't happen. Result is not getting updated. I've tried to introduce syntax error in function, Mathematica doesn't react at all.
The only way to force Mathematica to actually reload my package is to restart it.
It's driving me crazy, what am I doing wrong?
Upd
Such strange problems were caused by the fact that subpackages inside application depend on each other with Needs
instead of Get
. When I changed it to Get
the problems went away.
Answer
There isn't a fully general way to reload packages because packages can do just about anything in principle, not just create definitions.
I recommend that you write your package in a way that simply using Get
again will properly re-set its state. Assuming that all your package does is issue definitions, you could do something like this:
BeginPackage["MyPack`"];
Unprotect["MyPack`*"];
ClearAll["MyPack`*"];
ClearAll["MyPack`Private`*"];
Foo::usage = "";
Begin["`Private`"];
helper[x_] := x^2
Foo[x_] := 2*helper[x]
End[];
Protect["MyPack`*"];
EndPackage[];
But in general there's a lot more a package can do than issue definitions. For example, it can cache results (MaTeX), it can load or save some persistent configuration (MaTeX, MATLink), it can create temporary files and directories to work with, or some other kind of state that persists throughout the session, such as connecting to other programs (MATLink), it may load LibraryLink functions that must be unloaded before they can be safely reloaded (IGraph/M), etc. My point is that most packages that I contributed to had something else than definitions in them that made reloading problematic. Thus not all of them will fully reset their state when using Get
a second time, and sometimes I just need to manually quit the kernel before loading them again. But I do try to build in some robustness against reloading to make development easier (and accidental double loading by users should definitely not break things).
Comments
Post a Comment