@eval vs defining on module

In this question, users want to change the behavior of a function in Base.

What’s the difference between this post’s suggestion of

@eval Base begin

path_color = :red

function print_stackframe(io, i, frame::StackFrame, n::Int, digit_align_width, modulecolor)
...

vs another implementation

Base.path_color = :red
function Base.print_stackframe(io, i, frame::StackFrame, n::Int, digit_align_width, modulecolor)
...

or are they the same? Also what is the proper way to describe these implementations?

julia> StackFrame
ERROR: UndefVarError: StackFrame not defined

julia> @eval Base begin
           StackFrame
       end
Base.StackTraces.StackFrame

So if you don’t eval the code into Base you need to import all the functions/types etc from Base that you want to use.

I would call it “method overwriting”.

1 Like