I'm trying to figure out what is the best way to protect symbols within a package (a function's options in particular) so that no other value can be assigned to it.
Consider a function myFunc
:
Options[myFunc, OptionsPattern[]] = {op1->5}
myFunc[x_] :=
Module[{},
(* Function's core implementation *)
Print[OptionValue[op1]+2];
]
If I leave it as is, op1
is unprotected and anyone can assign a value to it.
That doesn't seem to affect myFunc
's functionality, but seems perhaps unprofessional (?).
Checking the symbol PlotRange
, for instance, turns out protected, as it is an option for Plot[]
and ListPlot[]
.
Up until now I've been protecting my options with (right before myFunc
's definition):
Unprotect[op1];
op1 = op1;
Protect[op1];
I must've seen that somewhere, because I don't remember coming up with it. Apart from the self definition op1=op1
, I understand it, but I'm curious to what the "correct" way to do it would be. And also, do I even need to protect it? Or is it just for clarity for the user (and other package makers), saying "Hey, this symbol is an option! Don't get it confused with your variables.".
EDIT: I forgot to mention. I'm also writing documentation for this application, in Wolfram Workbench. So, it's useful to have well defined protected symbols in general that I can document.
myFunc
and its options form a part of a package that you will publicly release, then I certainly would recommendProtect
ing it. If its something you will use on your own, it's up to you -- do you have a tendency to forget your option names, and assign values to them? $\endgroup$Op1
(forgot to do so in example), and I write my variables with lowercase letter, so there's very little chance for collision $\endgroup$OptionsPattern[]
inOptions[]
instead ofmyFunc
in your example code. $\endgroup$