Function Definition only if type is defined

I have a file of utilities functions I use a lot. I just include the file whenever I need those functions. In particular, I have functions defined for several different types. One of the types is defined in a separate package and not base julia. But I only sometimes want to load the package. I would like to keep the function definition in the same place since I have several versions of this function depending on the type. But it throws an error if the package is not loaded because the type isn’t defined. How do I only defined the function if the type is defined? How do people typically organize functions in cases like this?

You can you use @isdefined Type to check if the type is available in the current scope.

1 Like

Your might also find https://github.com/JuliaPackaging/Requires.jl useful in order to only define the relevant functions if the package is loaded.

3 Likes