Printing typemax as Inf


#1

I’d like add something to a Base method. In particular, I’d like to override Base.print for a particular type but still need to call the original function in most cases. The problem is that the original function is lost once I override it.
Any thoughts on how to do this?

To be more specific, I’d like to make Base.print show Inf and -Inf when the argument is typemax or typemin.


#2

You could use invoke but they’re are other (arguably better) ways of solving the problem.


#3

You can create a print method in your own module or in the Main module.
Start by defining

print(args...) = Base.print(args...)

Now you can define your own methods like

print(x::Int) = Base.print(x == typemax(Int) ? "Inf" : x)

Now import this print into every module where you want to change the behavior the function.
(You can use eval to inject code into a module without having to modify that modules’ source code.)
When you want to use the original function, you simply call Base.print.

Edit: I haven’t actually tried this. Now that I think about it, there will probably be world age issues.