Registering information to a module global dict using macros

Hello! I’m trying to get some convenience functionality working for a macro I’m working on. What I want to do is register a key and where this key occurs in both the internal package and external uses of the package. You can see the MWE below and what I’m seeing with different use cases. The documentation mentions this in the Module section + precompilation, but doesn’t give any details on how to accomplish this (afaict).

Ok. It needs to be a bit more complex to get the behaviour I’m seeing. In a package called MWE:

MWE/src/MWE.jl

module MWE

import Logging
const StrgDict = Dict{String, LineNumberNode}()

macro data(msg, exs...)
    StrgDict[msg] = __source__
    :($Logging.@info $msg $(exs...)) |> esc
end


function my_func(j)
    @data "Internal" j
end

end # module

MWE/ext/MyModule.jl

module MyModule
import MWE

function my_func()

    MWE.@data "Hello" x=1
end

function another_func(q)
    MWE.@data "Goodby" q
end

end

Then in a julia session w/ the project activated and ext added to the load path:

import:

julia> import MyModule
[ Info: Precompiling MyModule [top-level]

julia> MyModule.MWE.StrgDict
Dict{String, LineNumberNode} with 1 entry:
  "Internal" => :(#= /Users/Matt/Documents/Developer_Projects/JuliaDev/MWE/src/MWE.jl:13 =#)

include:

julia> include("ext/MyModule.jl")
[ Info: Precompiling MWE [02cddd85-a227-493d-88c6-f170f361fb05]
Main.MyModule

julia> MyModule.MWE.StrgDict
Dict{String, LineNumberNode} with 3 entries:
  "Internal" => :(#= /Users/Matt/Documents/Developer_Projects/JuliaDev/MWE/src/MWE.jl:13 =#)
  "Hello"    => :(#= /Users/Matt/Documents/Developer_Projects/JuliaDev/MWE/ext/MyModule.jl:7 =#)
  "Goodby"   => :(#= /Users/Matt/Documents/Developer_Projects/JuliaDev/MWE/ext/MyModule.jl:13 =#)
1 Like

For those coming here. The only solution is to have a storage solution in the module you are wanting to register things in and then call a registering function in your init. You can see this in the Automatic registering section and the ExampleProject of this repo: GitHub - mkschleg/ChoosyDataLoggers.jl: A fussy consumer of data.. So far, I don’t think there is another more automatic way to do this.