Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 37 additions & 4 deletions example/cool_example.jl
Original file line number Diff line number Diff line change
@@ -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()"
# 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"
24 changes: 23 additions & 1 deletion ext/DistributionsExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
3 changes: 3 additions & 0 deletions src/PackageExtensionsExample.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading