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:


module MWE

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

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

function my_func(j)
    @data "Internal" j

end # module


module MyModule
import MWE

function my_func()

    MWE.@data "Hello" x=1

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


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


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 =#)


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

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: So far, I don’t think there is another more automatic way to do this.