BeginPackage¶
Status: Stable
documented, exercised by the test suite and/or worked examples, with no known limitations recorded.
Description¶
BeginPackage["ctx"] sets the current context to "ctx" and restricts
BeginPackage["ctx", {"need1", ...}] additionally prepends the
Notes
$ContextPath to {"ctx\`", "System\`"}, matching the standard package prologue. listed contexts to $ContextPath.Examples (4)¶
Every input below was run against the current Mathilda build and its output recorded.
Applications (4)¶
In[1]:= $Context
Out[1]= "Global`"
In[2]:= BeginPackage["MyPkg`"]
Out[2]= "MyPkg`"
In[3]:= $Context
Out[3]= "MyPkg`"
In[4]:= $ContextPath
Out[4]= {"MyPkg`", "System`"}
Implementation notes¶
builtin_begin_package (src/context.c) accepts "ctx"and an optionalNeedslist of context strings, dispatching tocontext_begin_package. That pushes aFRAME_PACKAGEframe (snapshotting context and path), setsg_currentto the absolute package context (relative ``... ` contexts are rejected), clears the search path and rebuilds it as{ctx, System}plus any valid, non-duplicateNeedsentries.republish_statethen republishes$Context/$ContextPath, and the new context is returned.EndPackage[](context_end_package) pops the frame and prepends the closed context to$ContextPath. Invalid specs emitBeginPackage::cxt`.
Attributes: Protected.
References¶
- Source:
src/context.c - Specification:
docs/spec/builtins/scoping-constructs.md - Tests:
tests/test_context.c
Notes & additional examples¶
Notes¶
BeginPackage["ctx"]sets$Contexttoctx` and restricts$ContextPathto{"ctx", "System"}, matching the standard package prologue so only system symbols and the package's own symbols resolve under short names. Use the matchingEndPackage[]` to close it.