diff --git a/example/cool_example.jl b/example/cool_example.jl index cec7c84..fbf53c4 100644 --- a/example/cool_example.jl +++ b/example/cool_example.jl @@ -1,14 +1,47 @@ -cd(@__DIR__) +#= +You should be able run this script from any directory with any project settings. +=# + using Pkg -# run Pkg.instantiate() the first time you run this code -Pkg.activate("") + +cd(@__DIR__) + +# activates the project in the script's directory +Pkg.activate(".") + +# adds the local PackageExtensionsExample package to the script project (don't use Pkg.add here) +Pkg.develop(path="../") + +################################################################################################### using PackageExtensionsExample + # cool function should have no methods until Distributions is loaded @assert length(methods(cool_function)) == 0 +try + cool_function() +catch e + @assert e isa MethodError +end + +# calling @cool_macro here throws a LoadError at the module level +# @cool_macro + +@assert !isdefined(PackageExtensionsExample,:MyExtType) + + +################################################################################################### using Distributions # cool function should have 1 method now that Distributions is loaded @assert length(methods(cool_function)) == 1 cool_function() -# should print "calling cool_function()" \ No newline at end of file +# should print "calling cool_function()" + +@cool_macro +# should print "calling cool_macro()" + +@assert isdefined(PackageExtensionsExample,:MyExtType) + +println(PackageExtensionsExample.MyExtType()) +# should show message containing "hello from DistributionsExt" \ No newline at end of file diff --git a/ext/DistributionsExt.jl b/ext/DistributionsExt.jl index 3299259..63a6a0f 100644 --- a/ext/DistributionsExt.jl +++ b/ext/DistributionsExt.jl @@ -2,8 +2,13 @@ module DistributionsExt using PackageExtensionsExample using StatsBase - import PackageExtensionsExample: cool_function + import PackageExtensionsExample: cool_function, @cool_macro import Distributions: logpdf + export MyExtType + + @kwdef struct MyExtType + msg = "hello from DistributionsExt" + end function logpdf(d::MyType) @@ -15,4 +20,21 @@ module DistributionsExt println("calling cool_function()") end + macro cool_macro() + :(println("calling cool_macro()")) + end + + # this installs exported symbols such as MyExtType to the parent module. Using println instead of @info can save 300ms on compilation. + function __init__() + # The @spawn is a hack to work around the new mechanisms in Julia 1.11 (in 1.10 you don't need it). This is probably not 100% safe, not knowing if the module loading code being touched is thread safe. It shows what's happening until a better solution is found. + Threads.@spawn begin + sleep(0.001) + Core.eval(PackageExtensionsExample, + quote + println("Exporting symbols from DistributionsExt to PackageExtensionsExample") + ext = Base.get_extension(PackageExtensionsExample, :DistributionsExt) + using .ext + end) + end + end end diff --git a/src/PackageExtensionsExample.jl b/src/PackageExtensionsExample.jl index b30dcd6..c6526e0 100644 --- a/src/PackageExtensionsExample.jl +++ b/src/PackageExtensionsExample.jl @@ -2,8 +2,11 @@ module PackageExtensionsExample export MyType export cool_function + export @cool_macro struct MyType end function cool_function end + + macro cool_macro end end