From 58c55452c4089aafa62fb4c4e1257000bad87ef1 Mon Sep 17 00:00:00 2001 From: "FURRYSTATION\\kirby" Date: Thu, 24 Jul 2025 11:37:48 -0400 Subject: [PATCH 1/4] added macro example --- example/cool_example.jl | 31 +++++++++++++++++++++++++++---- ext/DistributionsExt.jl | 6 +++++- src/PackageExtensionsExample.jl | 3 +++ 3 files changed, 35 insertions(+), 5 deletions(-) diff --git a/example/cool_example.jl b/example/cool_example.jl index cec7c84..53af223 100644 --- a/example/cool_example.jl +++ b/example/cool_example.jl @@ -1,14 +1,37 @@ -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 + 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()" diff --git a/ext/DistributionsExt.jl b/ext/DistributionsExt.jl index 3299259..1d8c2f9 100644 --- a/ext/DistributionsExt.jl +++ b/ext/DistributionsExt.jl @@ -2,7 +2,7 @@ module DistributionsExt using PackageExtensionsExample using StatsBase - import PackageExtensionsExample: cool_function + import PackageExtensionsExample: cool_function, @cool_macro import Distributions: logpdf @@ -15,4 +15,8 @@ module DistributionsExt println("calling cool_function()") end + macro cool_macro() + :(println("calling cool_macro()")) + 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 From 58521fc07aeffe50dfbb5444e9c9bb4f73945e18 Mon Sep 17 00:00:00 2001 From: "FURRYSTATION\\kirby" Date: Thu, 24 Jul 2025 21:25:45 -0400 Subject: [PATCH 2/4] shows exporting of symbols to parent module --- example/cool_example.jl | 10 ++++++++++ ext/DistributionsExt.jl | 14 ++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/example/cool_example.jl b/example/cool_example.jl index 53af223..fbf53c4 100644 --- a/example/cool_example.jl +++ b/example/cool_example.jl @@ -12,6 +12,7 @@ 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 @@ -26,6 +27,10 @@ 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 @@ -35,3 +40,8 @@ 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 1d8c2f9..0f7dc4d 100644 --- a/ext/DistributionsExt.jl +++ b/ext/DistributionsExt.jl @@ -4,6 +4,11 @@ module DistributionsExt using StatsBase import PackageExtensionsExample: cool_function, @cool_macro import Distributions: logpdf + export MyExtType + + @kwdef struct MyExtType + msg = "hello from DistributionsExt" + end function logpdf(d::MyType) @@ -19,4 +24,13 @@ module DistributionsExt :(println("calling cool_macro()")) end + # this installs exported symbols such as MyExtType to the parent module + function __init__() + Core.eval(PackageExtensionsExample, + quote + @info("Exporting symbols from DistributionsExt to PackageExtensionsExample") + ext = Base.get_extension(PackageExtensionsExample, :DistributionsExt) + using .ext + end) + end end From df61580586cc246bceabec94cec13188cdf836a9 Mon Sep 17 00:00:00 2001 From: "FURRYSTATION\\kirby" Date: Sat, 26 Jul 2025 08:09:48 -0400 Subject: [PATCH 3/4] hack for Julia 1.11 --- ext/DistributionsExt.jl | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/ext/DistributionsExt.jl b/ext/DistributionsExt.jl index 0f7dc4d..7066ac0 100644 --- a/ext/DistributionsExt.jl +++ b/ext/DistributionsExt.jl @@ -24,13 +24,16 @@ module DistributionsExt :(println("calling cool_macro()")) end - # this installs exported symbols such as MyExtType to the parent module + # this installs exported symbols such as MyExtType to the parent module. Using println instead of @info can save 300ms on compilation. function __init__() - Core.eval(PackageExtensionsExample, - quote - @info("Exporting symbols from DistributionsExt to PackageExtensionsExample") - ext = Base.get_extension(PackageExtensionsExample, :DistributionsExt) - using .ext - end) + 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 From 83ca6ef8f81641e97edb1d37acabbb48e3cd2b12 Mon Sep 17 00:00:00 2001 From: "FURRYSTATION\\kirby" Date: Sat, 26 Jul 2025 08:17:52 -0400 Subject: [PATCH 4/4] comment on hack --- ext/DistributionsExt.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/ext/DistributionsExt.jl b/ext/DistributionsExt.jl index 7066ac0..63a6a0f 100644 --- a/ext/DistributionsExt.jl +++ b/ext/DistributionsExt.jl @@ -26,6 +26,7 @@ module DistributionsExt # 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,