Skip to content

Commit 41581ab

Browse files
authored
Merge pull request #2 from mind6/main
added macro example
2 parents 2f3410f + 83ca6ef commit 41581ab

File tree

3 files changed

+63
-5
lines changed

3 files changed

+63
-5
lines changed

example/cool_example.jl

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,47 @@
1-
cd(@__DIR__)
1+
#=
2+
You should be able run this script from any directory with any project settings.
3+
=#
4+
25
using Pkg
3-
# run Pkg.instantiate() the first time you run this code
4-
Pkg.activate("")
6+
7+
cd(@__DIR__)
8+
9+
# activates the project in the script's directory
10+
Pkg.activate(".")
11+
12+
# adds the local PackageExtensionsExample package to the script project (don't use Pkg.add here)
13+
Pkg.develop(path="../")
14+
15+
###################################################################################################
516
using PackageExtensionsExample
17+
618
# cool function should have no methods until Distributions is loaded
719
@assert length(methods(cool_function)) == 0
820

21+
try
22+
cool_function()
23+
catch e
24+
@assert e isa MethodError
25+
end
26+
27+
# calling @cool_macro here throws a LoadError at the module level
28+
# @cool_macro
29+
30+
@assert !isdefined(PackageExtensionsExample,:MyExtType)
31+
32+
33+
###################################################################################################
934
using Distributions
1035
# cool function should have 1 method now that Distributions is loaded
1136
@assert length(methods(cool_function)) == 1
1237

1338
cool_function()
14-
# should print "calling cool_function()"
39+
# should print "calling cool_function()"
40+
41+
@cool_macro
42+
# should print "calling cool_macro()"
43+
44+
@assert isdefined(PackageExtensionsExample,:MyExtType)
45+
46+
println(PackageExtensionsExample.MyExtType())
47+
# should show message containing "hello from DistributionsExt"

ext/DistributionsExt.jl

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,13 @@ module DistributionsExt
22

33
using PackageExtensionsExample
44
using StatsBase
5-
import PackageExtensionsExample: cool_function
5+
import PackageExtensionsExample: cool_function, @cool_macro
66
import Distributions: logpdf
7+
export MyExtType
8+
9+
@kwdef struct MyExtType
10+
msg = "hello from DistributionsExt"
11+
end
712

813

914
function logpdf(d::MyType)
@@ -15,4 +20,21 @@ module DistributionsExt
1520
println("calling cool_function()")
1621
end
1722

23+
macro cool_macro()
24+
:(println("calling cool_macro()"))
25+
end
26+
27+
# this installs exported symbols such as MyExtType to the parent module. Using println instead of @info can save 300ms on compilation.
28+
function __init__()
29+
# 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.
30+
Threads.@spawn begin
31+
sleep(0.001)
32+
Core.eval(PackageExtensionsExample,
33+
quote
34+
println("Exporting symbols from DistributionsExt to PackageExtensionsExample")
35+
ext = Base.get_extension(PackageExtensionsExample, :DistributionsExt)
36+
using .ext
37+
end)
38+
end
39+
end
1840
end

src/PackageExtensionsExample.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@ module PackageExtensionsExample
22

33
export MyType
44
export cool_function
5+
export @cool_macro
56

67
struct MyType end
78

89
function cool_function end
10+
11+
macro cool_macro end
912
end

0 commit comments

Comments
 (0)