gap icon indicating copy to clipboard operation
gap copied to clipboard

Add a hook to allow loading code when some other package is loaded

Open fingolfin opened this issue 3 years ago • 1 comments

I vaguely felt that I had suggested this before, but I just couldn't find an issue for it, so here goes again:

It would be great if there was a function InstallPackageLoadedCallback (better name suggestions welcome) similar to InstallAtExit except that it registers a callback that is called whenever a package is "completely" loaded.

Then code like

if not IsPackageMarkedForLoading( "aclib" , "1.0" ) then
    POL_AlmostCrystallographicGroup := false;
else
    POL_AlmostCrystallographicGroup := function( a,b,c )
       local G, mats;
       G := AlmostCrystallographicGroup(a,b,c);
       mats := GeneratorsOfGroup( G );
       G := Group( mats );
       return G;
    end;
fi;

could be changed to use that function, say like this (which is not 100% right, but hopefully conveys the idea):

    POL_AlmostCrystallographicGroup := false;
    InstallPackageLoadedCallback(["aclib"],
        function(pkgname)
           POL_AlmostCrystallographicGroup" := function( a,b,c )
               local G, mats;
               G := AlmostCrystallographicGroup(a,b,c);
               mats := GeneratorsOfGroup( G );
               G := Group( mats );
               return G;
            end;
            return true;
        end
    );

Here the first argument is a list of packages in which we are interested; the callback is only called when one of these is loaded. It gets as argument the name of the package that was loaded (actually I am not 100% sure this is even necessary or that useful; and perhaps it should actually be a list of packages that were loaded; so that if three packages were loaded simultaneously, we call this once, not three times. But that's in the end a minor detail, I think).

The return value of the callback should indicate whether to keep the callback around; or whether it is done with its job and can be removed. In the example above I used true to indicate "I'm done, remove me".

fingolfin avatar Mar 21 '22 00:03 fingolfin

The first argument to InstallPackageLoadedCallback could also be a list of packages, or even a list of pairs "package name -- version", as in the NeededOtherPackages of a package

fingolfin avatar Sep 23 '22 08:09 fingolfin

Resolve by PR #5375

fingolfin avatar Oct 17 '23 07:10 fingolfin