Exposing Symbols to $ContextPath

后端 未结 2 1459
轻奢々
轻奢々 2021-02-02 10:57

There are a variety of Internal`context functions that are useful, such as InheritedBlock, Bag and StuffBag, etc., as well as many useful Developer` fu

相关标签:
2条回答
  • 2021-02-02 11:37

    This is IMO a very deep and valid question, and judging by the vote count, I am not alone in thinking this. If we had this feature fully supported on the language level, this would give one a whole new way to manipulate the scoping and encapsulation, and would IMO often allow for a cleaner code and better information hiding. This would be similar to from module-name import name1,name2,... in Python.

    Perhaps as many of us, I have tried multiple approaches, but all of them seem fragile and incomplete. The worst case is for packages, for which I have no good solution. For the interactive work in the FrontEnd, here is one which might be acceptable. First define a general macro to make the literal substitutions:

    ClearAll[withImported];
    SetAttributes[withImported, HoldAll];
    withImported[importingRules : {(_Symbol ->  _String) ..}, code_] :=
      With @@ Join[
         Hold[importingRules] /.
          (name_Symbol -> context_String) :>
              With[{eval =
                   Block[{$ContextPath = Append[$ContextPath, context]},
                     ToExpression[context <> ToString[Unevaluated@name]]
                   ]},
                 Set[name, eval] /; True],
         Hold[code]];
    
    withImported[importingRules : {({__Symbol} -> _String) ..}, code_] :=  
        Reverse[Hold @@ Flatten[Unevaluated[importingRules] /.
            ({syms__Symbol} -> s_String) :> Thread[s :> {syms}]], {2}] /. 
            RuleDelayed -> Rule /.
            Hold[expandedRules : ((_Symbol ->  _String) ..)] :> 
                 withImported[{expandedRules}, code];
    

    Then, create a function which would incorporate your favorite shortcuts, for example:

    shortcutF = 
       Function[code,
         withImported[
           {
             {PackedArrayQ, ToPackedArray, FromPackedArray} -> "Developer`",
             {InheritedBlock, WithLocalSettings} -> "Internal`"
           },
           code
         ],
         HoldAll];
    

    You can now wrap your code in shortcutF and start using the short names. Up to now, this would also work for packages, but you will have to wrap all your code (or at least those pieces which contain short-cuts) in shortcutF, which is not very convenient. As a further convenience step, you may assign the above function to $Pre:

    $Pre = shortcutF;
    

    Here are some examples of use:

    In[31]:= 
    WithLocalSettings[Null,Abort[],Print["Cleanup"]]
    
    During evaluation of In[31]:= Cleanup
    Out[31]= $Aborted[]
    
    In[32]:= PackedArrayQ[Range[10]]
    Out[32]= True
    
    In[33]:= PackedArrayQ@FromPackedArray[Range[10]]
    Out[33]= False
    

    Since With is used under the cover, what really happens is that your short-cut symbols get substituted by fully-qualified symbol names, before the code is executed.

    This is as far as I could get, but this feature seems to be particularly crying for a native support from the language.

    0 讨论(0)
  • 2021-02-02 11:49

    A variation of Leonid's answer which applies on an earlier stage:

    InternalSymbols={"Bag","BagLength","BagPart","StuffBag"}
    $PreRead=#/.(InternalSymbols/.{s_String:>s->"Internal`"<>s})&
    

    After typing this in a notebook, typing

    ?Bag
    

    gives me

    Internal`Bag
    Attributes[Internal`Bag]={Protected}
    

    while

    ?AbsSquare
    

    gives

    Information::notfound: Symbol AbsSquare not found.
    

    but

    ?Internal`AbsSquare
    

    gives

    Internal`AbsSquare
    Attributes[Internal`AbsSquare]={Listable,NumericFunction,Protected}
    

    However it seems to only work in the notebook interface, not when using math on the command line.

    0 讨论(0)
提交回复
热议问题