I'm having issues with inherited options for functions I defined in a package file. Setting new default options doesn't quite work as I expect. Also, a function which I'd like to stay private to the package somehow leaks into the public context…
Consider the package file OptionInheritanceTest.m
:
BeginPackage["OptionInheritanceTest`"]
f2::usage = ""
f3::usage = ""
(* Default options *)
Options[f1] = {Flag1 -> True};
Options[f2] = Join[Options[f1],
{Flag2 -> True}];
Options[f3] = Join[{Method -> "A"},
Options[f2]];
Begin["`Private`"]
f1[expr_,
opts:OptionsPattern[]] := {expr,
OptionValue[Flag1]};
f2[expr_,
opts:OptionsPattern[]] := Append[f1[expr,
FilterRules[{opts},
Options[f1]]],
OptionValue[Flag2]];
f3[expr_,
opts:OptionsPattern[]] := Append[f2[expr,
FilterRules[{opts},
Options[f2]]],
OptionValue[Method]];
End[]
EndPackage[]
After loading this package in a notebook the commands
Options[f1]
f1[x]
Options[f2]
f2[x]
Options[f3]
f3[x]
return exactly the desired output. Let's try to set new default options for f3:
SetOptions[f3, Flag1 -> False, Flag2 -> False, Method -> "C"];
While Options[f3]
perfectly reflects the new option values the command
f3[x]
yields
{x, True, True, "C"}
So the new f3 defaults are not handed down to f1 and f2 when they're called by f3. How can I achieve that?
Also, I would like f1 to remain in the private context, but Options[f1] = …
seems to export it to the public context. Simply declaring this option in the private block wrecks the option inheritance unfortunately. What is the way to go forward?