Given the following code in a file called “TestMacro.jl”:
module TestMacro
import Base
struct Glob end
export Glob
macro define()
:(Base.:*(a::Glob, b::Glob) = "here")
end
@define
Base.:*(a::Glob, b::Glob) = "there"
end # module TestMacro
why is the output of this code “here” instead of “there”?
julia> using TestMacro
[ Info: Precompiling TestMacro [85f89ff2-3dec-4fa1-bb05-16c45dba80a1]
WARNING: Method definition *(TestMacro.Glob, TestMacro.Glob) in module
TestMacro at :\Users\seatt\OneDrive\Documents\temp\TestMacro\src\TestMacro.jl:7
overwritten at c:\Users\seatt\OneDrive\Documents\temp\TestMacro\src\TestMacro.jl:12.
** incremental compilation may be fatally broken for this module **
julia> a = Glob()
Glob()
julia> a*a
"here"
I expected the second definition of Base.:*
would supersede that created by the call of the @define
macro, since it comes after it in the file and there is a compiler warning saying that the times method has been redefined.
If the code is included in a fresh Julia session it executes as I would expect.
julia> include("src/TestMacro.jl")
Main.TestMacro
julia> a = TestMacro.Glob()
Main.TestMacro.Glob()
julia> a*a
"there"
Presumably this is caused by a difference between the way expressions are handled in the REPL vs. non-REPL. But what is the mental model one should use to predict the outcome?