What to be aware when using new-style package?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
4
down vote

favorite
2












A new package format is described in this post. This format does not use BeginPackage and Begin. Instead, each package file is scanned before its contents are evaluated, and the context of each symbol is decided beforehand.



What are some of the things to be aware when using this package format?










share|improve this question

























    up vote
    4
    down vote

    favorite
    2












    A new package format is described in this post. This format does not use BeginPackage and Begin. Instead, each package file is scanned before its contents are evaluated, and the context of each symbol is decided beforehand.



    What are some of the things to be aware when using this package format?










    share|improve this question























      up vote
      4
      down vote

      favorite
      2









      up vote
      4
      down vote

      favorite
      2






      2





      A new package format is described in this post. This format does not use BeginPackage and Begin. Instead, each package file is scanned before its contents are evaluated, and the context of each symbol is decided beforehand.



      What are some of the things to be aware when using this package format?










      share|improve this question













      A new package format is described in this post. This format does not use BeginPackage and Begin. Instead, each package file is scanned before its contents are evaluated, and the context of each symbol is decided beforehand.



      What are some of the things to be aware when using this package format?







      packages






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 1 hour ago









      Szabolcs

      155k13418906




      155k13418906




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          3
          down vote













          I will give a FAQ-style presentation of some things I became aware while transitioning one of my packages to this format. Leonid's description of the new package format is required reading before looking at this FAQ!



          How are contexts assigned to symbols in the package?



          In traditional packages, symbols are looked up from $ContextPath, and if not found, created in $Context. It is always the current value of $ContextPath and $Context that determines what a symbol name in the package file refers to.



          BeginPackage and Begin simply manipulate $ContextPath and $Context. When they are evaluated, the value of these system variables will change. End and EndPackage change it back. The process is dynamic, and happens as each line in the package file is evaluated one by one.



          In new-style packages, the context of each symbol is decided before the contents of the package file are evaluated. Mathematica will scan all files in the package directory for the Package, PackageExport, PackageImport, PackageScope directives, and uses them to pre-determine the context of each symbol that appears in the package.



          The package contents are evaluated only after this has happened.



          Which contexts are symbols created in?



          A private symbol in a file named Foo that is part of the package MyApp` will go in the context MyApp`Foo`PackagePrivate`.



          A package scope symbol, declared with PackageScope, will go in MyApp`PackageScope`.



          An exported symbol, declared with PackageExport, will go in MyApp`.



          How do Package, PackageExport, PackageImport, PackageScope evaluate?



          They do not evaluate at all.



          They behave more like directives than symbols. They simply signal to the parser how to assign contexts to each symbol in the file. This assignment of contexts takes place before any evaluation is done.



          This means that e.g. this is not valid:



          PackageExport /@ "symbol1", "symbol2"


          Instead we must write



          PackageExport["symbol1"]
          PackageExport["symbol2"]


          PackageExport never evaluates as a symbol.



          In what order are files loaded?



          A new-style package is usually made up by multiple files, each containing Package["MyApp`"]. If the loading of one of these files is triggered with Get or Needs, all other files that belong to the MyApp` package will be loaded.



          After the first file was loaded, the rest will be loaded in alphabetical order.



          The first file would either have the same name as the package itself (e.g. MyApp.m for MyApp`) or it would be explicitly loaded in Kernel/init.m.



          What are valid file names?



          Since file names are mapped to context names, file names must also be valid context names. This means that _ or a space cannot be used in file names. File names may not start with a digit.



          It is a natural though to try to control loading order by prepending digits to file names. But such names are no valid.



          What is the value of $Context and $ContextPath during package loading?



          In Mathematica 11.0 and later, the value of $Context and $ContextPath correspond to the file that is currently being loaded.



          For example, if the current file is Main.m and the package name is MyApp`, then



          $Context === "MyApp`Main`PackagePrivate`"
          $
          ContextPath === "MyApp`PackageScope`", "MyApp`", "System`"


          However, in Mathematica 10.x, $Context and $ContextPath do not change at all during loading. They retain the value they had before package loading (e.g. $Context === Global` and the usual $ContextPath).



          What this means in practice is that in Mathematica 10.x, ToExpression["x"] would create the symbol x in the Global` context (or whatever was the context before package loading) instead of the package's private context.



          The same is true for <*expr*> included in StringTemplates.



          Finally, if the package loads another file with Get, all symbols in that file will be created in Global`, not in the package's private context.



          What is the value of $InputFile and $Input when loading new-style packages?



          In Mathematica 11.0 and later, $InputFile is always set to the specific file that is currently being loaded. It is different for each file that makes up the package.



          In Mathematica 10.x, $InputFile is always set to the first file of the package, regardless of which file is currently being loaded.



          $Input is always set to the name of the first file, in all versions between M10.0–M11.3.



          Can we give a definition to Package?



          Each file that is part of the package must include



          Package["MyContext`"]


          As explained before Package is just a directive, and does not have a definition. We might, in principle, give it a definition, and hope that it will evaluate in each package file.



          This, however, does not happen. Package and related directives are removed from the file before its contents are evaluated.






          share|improve this answer






















            Your Answer




            StackExchange.ifUsing("editor", function ()
            return StackExchange.using("mathjaxEditing", function ()
            StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
            StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
            );
            );
            , "mathjax-editing");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "387"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f184711%2fwhat-to-be-aware-when-using-new-style-package%23new-answer', 'question_page');

            );

            Post as a guest






























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            3
            down vote













            I will give a FAQ-style presentation of some things I became aware while transitioning one of my packages to this format. Leonid's description of the new package format is required reading before looking at this FAQ!



            How are contexts assigned to symbols in the package?



            In traditional packages, symbols are looked up from $ContextPath, and if not found, created in $Context. It is always the current value of $ContextPath and $Context that determines what a symbol name in the package file refers to.



            BeginPackage and Begin simply manipulate $ContextPath and $Context. When they are evaluated, the value of these system variables will change. End and EndPackage change it back. The process is dynamic, and happens as each line in the package file is evaluated one by one.



            In new-style packages, the context of each symbol is decided before the contents of the package file are evaluated. Mathematica will scan all files in the package directory for the Package, PackageExport, PackageImport, PackageScope directives, and uses them to pre-determine the context of each symbol that appears in the package.



            The package contents are evaluated only after this has happened.



            Which contexts are symbols created in?



            A private symbol in a file named Foo that is part of the package MyApp` will go in the context MyApp`Foo`PackagePrivate`.



            A package scope symbol, declared with PackageScope, will go in MyApp`PackageScope`.



            An exported symbol, declared with PackageExport, will go in MyApp`.



            How do Package, PackageExport, PackageImport, PackageScope evaluate?



            They do not evaluate at all.



            They behave more like directives than symbols. They simply signal to the parser how to assign contexts to each symbol in the file. This assignment of contexts takes place before any evaluation is done.



            This means that e.g. this is not valid:



            PackageExport /@ "symbol1", "symbol2"


            Instead we must write



            PackageExport["symbol1"]
            PackageExport["symbol2"]


            PackageExport never evaluates as a symbol.



            In what order are files loaded?



            A new-style package is usually made up by multiple files, each containing Package["MyApp`"]. If the loading of one of these files is triggered with Get or Needs, all other files that belong to the MyApp` package will be loaded.



            After the first file was loaded, the rest will be loaded in alphabetical order.



            The first file would either have the same name as the package itself (e.g. MyApp.m for MyApp`) or it would be explicitly loaded in Kernel/init.m.



            What are valid file names?



            Since file names are mapped to context names, file names must also be valid context names. This means that _ or a space cannot be used in file names. File names may not start with a digit.



            It is a natural though to try to control loading order by prepending digits to file names. But such names are no valid.



            What is the value of $Context and $ContextPath during package loading?



            In Mathematica 11.0 and later, the value of $Context and $ContextPath correspond to the file that is currently being loaded.



            For example, if the current file is Main.m and the package name is MyApp`, then



            $Context === "MyApp`Main`PackagePrivate`"
            $
            ContextPath === "MyApp`PackageScope`", "MyApp`", "System`"


            However, in Mathematica 10.x, $Context and $ContextPath do not change at all during loading. They retain the value they had before package loading (e.g. $Context === Global` and the usual $ContextPath).



            What this means in practice is that in Mathematica 10.x, ToExpression["x"] would create the symbol x in the Global` context (or whatever was the context before package loading) instead of the package's private context.



            The same is true for <*expr*> included in StringTemplates.



            Finally, if the package loads another file with Get, all symbols in that file will be created in Global`, not in the package's private context.



            What is the value of $InputFile and $Input when loading new-style packages?



            In Mathematica 11.0 and later, $InputFile is always set to the specific file that is currently being loaded. It is different for each file that makes up the package.



            In Mathematica 10.x, $InputFile is always set to the first file of the package, regardless of which file is currently being loaded.



            $Input is always set to the name of the first file, in all versions between M10.0–M11.3.



            Can we give a definition to Package?



            Each file that is part of the package must include



            Package["MyContext`"]


            As explained before Package is just a directive, and does not have a definition. We might, in principle, give it a definition, and hope that it will evaluate in each package file.



            This, however, does not happen. Package and related directives are removed from the file before its contents are evaluated.






            share|improve this answer


























              up vote
              3
              down vote













              I will give a FAQ-style presentation of some things I became aware while transitioning one of my packages to this format. Leonid's description of the new package format is required reading before looking at this FAQ!



              How are contexts assigned to symbols in the package?



              In traditional packages, symbols are looked up from $ContextPath, and if not found, created in $Context. It is always the current value of $ContextPath and $Context that determines what a symbol name in the package file refers to.



              BeginPackage and Begin simply manipulate $ContextPath and $Context. When they are evaluated, the value of these system variables will change. End and EndPackage change it back. The process is dynamic, and happens as each line in the package file is evaluated one by one.



              In new-style packages, the context of each symbol is decided before the contents of the package file are evaluated. Mathematica will scan all files in the package directory for the Package, PackageExport, PackageImport, PackageScope directives, and uses them to pre-determine the context of each symbol that appears in the package.



              The package contents are evaluated only after this has happened.



              Which contexts are symbols created in?



              A private symbol in a file named Foo that is part of the package MyApp` will go in the context MyApp`Foo`PackagePrivate`.



              A package scope symbol, declared with PackageScope, will go in MyApp`PackageScope`.



              An exported symbol, declared with PackageExport, will go in MyApp`.



              How do Package, PackageExport, PackageImport, PackageScope evaluate?



              They do not evaluate at all.



              They behave more like directives than symbols. They simply signal to the parser how to assign contexts to each symbol in the file. This assignment of contexts takes place before any evaluation is done.



              This means that e.g. this is not valid:



              PackageExport /@ "symbol1", "symbol2"


              Instead we must write



              PackageExport["symbol1"]
              PackageExport["symbol2"]


              PackageExport never evaluates as a symbol.



              In what order are files loaded?



              A new-style package is usually made up by multiple files, each containing Package["MyApp`"]. If the loading of one of these files is triggered with Get or Needs, all other files that belong to the MyApp` package will be loaded.



              After the first file was loaded, the rest will be loaded in alphabetical order.



              The first file would either have the same name as the package itself (e.g. MyApp.m for MyApp`) or it would be explicitly loaded in Kernel/init.m.



              What are valid file names?



              Since file names are mapped to context names, file names must also be valid context names. This means that _ or a space cannot be used in file names. File names may not start with a digit.



              It is a natural though to try to control loading order by prepending digits to file names. But such names are no valid.



              What is the value of $Context and $ContextPath during package loading?



              In Mathematica 11.0 and later, the value of $Context and $ContextPath correspond to the file that is currently being loaded.



              For example, if the current file is Main.m and the package name is MyApp`, then



              $Context === "MyApp`Main`PackagePrivate`"
              $
              ContextPath === "MyApp`PackageScope`", "MyApp`", "System`"


              However, in Mathematica 10.x, $Context and $ContextPath do not change at all during loading. They retain the value they had before package loading (e.g. $Context === Global` and the usual $ContextPath).



              What this means in practice is that in Mathematica 10.x, ToExpression["x"] would create the symbol x in the Global` context (or whatever was the context before package loading) instead of the package's private context.



              The same is true for <*expr*> included in StringTemplates.



              Finally, if the package loads another file with Get, all symbols in that file will be created in Global`, not in the package's private context.



              What is the value of $InputFile and $Input when loading new-style packages?



              In Mathematica 11.0 and later, $InputFile is always set to the specific file that is currently being loaded. It is different for each file that makes up the package.



              In Mathematica 10.x, $InputFile is always set to the first file of the package, regardless of which file is currently being loaded.



              $Input is always set to the name of the first file, in all versions between M10.0–M11.3.



              Can we give a definition to Package?



              Each file that is part of the package must include



              Package["MyContext`"]


              As explained before Package is just a directive, and does not have a definition. We might, in principle, give it a definition, and hope that it will evaluate in each package file.



              This, however, does not happen. Package and related directives are removed from the file before its contents are evaluated.






              share|improve this answer
























                up vote
                3
                down vote










                up vote
                3
                down vote









                I will give a FAQ-style presentation of some things I became aware while transitioning one of my packages to this format. Leonid's description of the new package format is required reading before looking at this FAQ!



                How are contexts assigned to symbols in the package?



                In traditional packages, symbols are looked up from $ContextPath, and if not found, created in $Context. It is always the current value of $ContextPath and $Context that determines what a symbol name in the package file refers to.



                BeginPackage and Begin simply manipulate $ContextPath and $Context. When they are evaluated, the value of these system variables will change. End and EndPackage change it back. The process is dynamic, and happens as each line in the package file is evaluated one by one.



                In new-style packages, the context of each symbol is decided before the contents of the package file are evaluated. Mathematica will scan all files in the package directory for the Package, PackageExport, PackageImport, PackageScope directives, and uses them to pre-determine the context of each symbol that appears in the package.



                The package contents are evaluated only after this has happened.



                Which contexts are symbols created in?



                A private symbol in a file named Foo that is part of the package MyApp` will go in the context MyApp`Foo`PackagePrivate`.



                A package scope symbol, declared with PackageScope, will go in MyApp`PackageScope`.



                An exported symbol, declared with PackageExport, will go in MyApp`.



                How do Package, PackageExport, PackageImport, PackageScope evaluate?



                They do not evaluate at all.



                They behave more like directives than symbols. They simply signal to the parser how to assign contexts to each symbol in the file. This assignment of contexts takes place before any evaluation is done.



                This means that e.g. this is not valid:



                PackageExport /@ "symbol1", "symbol2"


                Instead we must write



                PackageExport["symbol1"]
                PackageExport["symbol2"]


                PackageExport never evaluates as a symbol.



                In what order are files loaded?



                A new-style package is usually made up by multiple files, each containing Package["MyApp`"]. If the loading of one of these files is triggered with Get or Needs, all other files that belong to the MyApp` package will be loaded.



                After the first file was loaded, the rest will be loaded in alphabetical order.



                The first file would either have the same name as the package itself (e.g. MyApp.m for MyApp`) or it would be explicitly loaded in Kernel/init.m.



                What are valid file names?



                Since file names are mapped to context names, file names must also be valid context names. This means that _ or a space cannot be used in file names. File names may not start with a digit.



                It is a natural though to try to control loading order by prepending digits to file names. But such names are no valid.



                What is the value of $Context and $ContextPath during package loading?



                In Mathematica 11.0 and later, the value of $Context and $ContextPath correspond to the file that is currently being loaded.



                For example, if the current file is Main.m and the package name is MyApp`, then



                $Context === "MyApp`Main`PackagePrivate`"
                $
                ContextPath === "MyApp`PackageScope`", "MyApp`", "System`"


                However, in Mathematica 10.x, $Context and $ContextPath do not change at all during loading. They retain the value they had before package loading (e.g. $Context === Global` and the usual $ContextPath).



                What this means in practice is that in Mathematica 10.x, ToExpression["x"] would create the symbol x in the Global` context (or whatever was the context before package loading) instead of the package's private context.



                The same is true for <*expr*> included in StringTemplates.



                Finally, if the package loads another file with Get, all symbols in that file will be created in Global`, not in the package's private context.



                What is the value of $InputFile and $Input when loading new-style packages?



                In Mathematica 11.0 and later, $InputFile is always set to the specific file that is currently being loaded. It is different for each file that makes up the package.



                In Mathematica 10.x, $InputFile is always set to the first file of the package, regardless of which file is currently being loaded.



                $Input is always set to the name of the first file, in all versions between M10.0–M11.3.



                Can we give a definition to Package?



                Each file that is part of the package must include



                Package["MyContext`"]


                As explained before Package is just a directive, and does not have a definition. We might, in principle, give it a definition, and hope that it will evaluate in each package file.



                This, however, does not happen. Package and related directives are removed from the file before its contents are evaluated.






                share|improve this answer














                I will give a FAQ-style presentation of some things I became aware while transitioning one of my packages to this format. Leonid's description of the new package format is required reading before looking at this FAQ!



                How are contexts assigned to symbols in the package?



                In traditional packages, symbols are looked up from $ContextPath, and if not found, created in $Context. It is always the current value of $ContextPath and $Context that determines what a symbol name in the package file refers to.



                BeginPackage and Begin simply manipulate $ContextPath and $Context. When they are evaluated, the value of these system variables will change. End and EndPackage change it back. The process is dynamic, and happens as each line in the package file is evaluated one by one.



                In new-style packages, the context of each symbol is decided before the contents of the package file are evaluated. Mathematica will scan all files in the package directory for the Package, PackageExport, PackageImport, PackageScope directives, and uses them to pre-determine the context of each symbol that appears in the package.



                The package contents are evaluated only after this has happened.



                Which contexts are symbols created in?



                A private symbol in a file named Foo that is part of the package MyApp` will go in the context MyApp`Foo`PackagePrivate`.



                A package scope symbol, declared with PackageScope, will go in MyApp`PackageScope`.



                An exported symbol, declared with PackageExport, will go in MyApp`.



                How do Package, PackageExport, PackageImport, PackageScope evaluate?



                They do not evaluate at all.



                They behave more like directives than symbols. They simply signal to the parser how to assign contexts to each symbol in the file. This assignment of contexts takes place before any evaluation is done.



                This means that e.g. this is not valid:



                PackageExport /@ "symbol1", "symbol2"


                Instead we must write



                PackageExport["symbol1"]
                PackageExport["symbol2"]


                PackageExport never evaluates as a symbol.



                In what order are files loaded?



                A new-style package is usually made up by multiple files, each containing Package["MyApp`"]. If the loading of one of these files is triggered with Get or Needs, all other files that belong to the MyApp` package will be loaded.



                After the first file was loaded, the rest will be loaded in alphabetical order.



                The first file would either have the same name as the package itself (e.g. MyApp.m for MyApp`) or it would be explicitly loaded in Kernel/init.m.



                What are valid file names?



                Since file names are mapped to context names, file names must also be valid context names. This means that _ or a space cannot be used in file names. File names may not start with a digit.



                It is a natural though to try to control loading order by prepending digits to file names. But such names are no valid.



                What is the value of $Context and $ContextPath during package loading?



                In Mathematica 11.0 and later, the value of $Context and $ContextPath correspond to the file that is currently being loaded.



                For example, if the current file is Main.m and the package name is MyApp`, then



                $Context === "MyApp`Main`PackagePrivate`"
                $
                ContextPath === "MyApp`PackageScope`", "MyApp`", "System`"


                However, in Mathematica 10.x, $Context and $ContextPath do not change at all during loading. They retain the value they had before package loading (e.g. $Context === Global` and the usual $ContextPath).



                What this means in practice is that in Mathematica 10.x, ToExpression["x"] would create the symbol x in the Global` context (or whatever was the context before package loading) instead of the package's private context.



                The same is true for <*expr*> included in StringTemplates.



                Finally, if the package loads another file with Get, all symbols in that file will be created in Global`, not in the package's private context.



                What is the value of $InputFile and $Input when loading new-style packages?



                In Mathematica 11.0 and later, $InputFile is always set to the specific file that is currently being loaded. It is different for each file that makes up the package.



                In Mathematica 10.x, $InputFile is always set to the first file of the package, regardless of which file is currently being loaded.



                $Input is always set to the name of the first file, in all versions between M10.0–M11.3.



                Can we give a definition to Package?



                Each file that is part of the package must include



                Package["MyContext`"]


                As explained before Package is just a directive, and does not have a definition. We might, in principle, give it a definition, and hope that it will evaluate in each package file.



                This, however, does not happen. Package and related directives are removed from the file before its contents are evaluated.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited 40 mins ago


























                community wiki





                4 revs
                Szabolcs




























                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f184711%2fwhat-to-be-aware-when-using-new-style-package%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Comments

                    Popular posts from this blog

                    What does second last employer means? [closed]

                    Installing NextGIS Connect into QGIS 3?

                    One-line joke