6
$\begingroup$

This question already has an answer here:

I've been working on a Mathematica package (call it QuantumDot) that has become huge. Right now, all the back-end helper functions are in context QuantumDot`Private, and the WHOLE code is in one gigantic .m file.

I'd like to reorganize it so that QuantumDot has multiple sub-contexts corresponding, roughly, to each front-end function. I want to put all the back-end helper functions into these sub-contexts.

For example:

(*These are the main functions accessible to the user*)
QuantumDot`
   MainFunction[x,y,z]
   ComplexRoutine[x,y]

(*These are the backend helper functions*)
QuantumDot`MainFunction`
   internalFunction[a,b,c]
   addAndSubtract[a,b,c]
   distribute[a,b,c]
   ...

QuantumDot`ComplexRoutine`
   step1function[a,b,c]
   step2function[a,b,c]
   reducer[a,b,c]
   ...

I can achieve this by replacing in my existing code Begin["`Private`"] with Begin["`MainFunction`"], Begin["`ComplexRoutine `"],... as necessary.

BeginPackage["QuantumDot`"];
MainFunction::usage = "MainFunction[x,y,z] is a front-end function for users";

Begin["`MainFunction`"];  (*  was Begin["`Private`"];  *)

  internalFunction[a_,b_,c_] := a^2 - b^2 - c^2;
  addAndSubtract[a_,b_,c_] := a + b - c;
  distribute[a,b,c] := a(b+c)//Expand;

  MainFunction[x_,y_,z_]:=internalFunction[x,y,z]+addAndSubtract[x,y,z](*...*);

End[];

Begin["`ComplexRoutine`"];
  (*...*)
End[];

EndPackage[];

The problem is that the autocomplete picks up the back-end functions. (Apparently, there was something special with the "Private" context).

enter image description here

  1. What can I do to prevent the autocompletion feature from displaying these internal functions?

  2. I then intend to split my package into multiple .m files according to the main functions of the package. Is this a reasonable way to proceed?

$\endgroup$

marked as duplicate by QuantumDot, m_goldberg, Community May 28 '17 at 23:30

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.

  • 1
    $\begingroup$ Would it be of any help if you wrapped all those subcontexts within a Private context instead? $\endgroup$ – MarcoB May 21 '16 at 15:26
  • $\begingroup$ @MarcoB I thought about this a while back, and if I can't find a cleaner solution, I'll have to do what you suggest. $\endgroup$ – QuantumDot May 21 '16 at 15:54
  • $\begingroup$ Yeah, that's sort of what the Private context is meant to be doing for you. Is there some specific reason why you don't want to use it? $\endgroup$ – Emilio Pisanty May 23 '16 at 15:01
  • 1
    $\begingroup$ But Private can be included at any point in the context chain, so QuantumDot`Private`MainFunction` and QuantumDot`MainFunction`Private` are both valid contexts (called using Begin["`MainFunction`Private`"] and its reverse), and neither of them gets displayed on autocomplete. That enables you to have further organization inside of Private contexts and still shield those symbols from eyes that shouldn't see them. $\endgroup$ – Emilio Pisanty May 23 '16 at 16:16
  • 1
    $\begingroup$ @Emilio Shouldn't that be posted as an Answer? $\endgroup$ – Mr.Wizard May 24 '16 at 12:51
5
$\begingroup$

This is sort of what the `Private` context is meant to be doing for you. You write that you

just need more contexts that can behave like Private context for organizational purposes,

and this can be achieved using subcontexts that use a `Private` mark. My normal instinct would be to put the helper functions to MainFunction inside a `Private` branch of the `MainFunction` context branch,

Begin["`MainFunction`Private`"]

but you can also do it the other way around and have a master `Private` branch with lots of sub-branches, as

Begin["`Private`MainFunction`"]
$\endgroup$
  • $\begingroup$ (+1) This is a viable alternative, but I guess it doesn't technically answer the question. $\endgroup$ – QuantumDot May 26 '16 at 13:12
  • $\begingroup$ ... which is why I posted it as a comment to see if there were specific reasons why it didn't fit the bill. $\endgroup$ – Emilio Pisanty May 26 '16 at 16:25

Not the answer you're looking for? Browse other questions tagged or ask your own question.