Include file differently when within a module

Hi !

I want to include a .jl file in two different ways, according as it is included within a module or on its own. I know this sounds confusing ; here is a way to do it, which I think is awkward, and I’m looking for something more elegant.

so i have foo.jl with

macro in_my_module_only(arg)
    if @isdefined SOME_CRAZY_NAME
        return arg
    else
        return nothing
    end
end

@in_my_module_only begin ... end

and then “MyModule.jl” goes like this:

module MyModule
    SOME_CRAZY_NAME= 1
    include("foo.jl")
end

This actually works! However, I’m not sure if this is the right way to go about it. I thought of inspecting the current module with mod= @__MODULE__, but then I’m not sure what to do. If I inspect string(mod) I get Main.MyModule typically, but in other situations it would be different… or perhaps I should check that string(@__MODULE__) contains the substring MyModule ?

what do you think?

thanks!
Pierre

The module of the REPL is called “Main”. So you could check against that.

1 Like