Hi,

I'd like to understand how one can surgically add a piece to a long type 
chain of something like string in julia.

Say I want to update the printing of expression objects. 

So:

module M
import Base.string
function string(ex::Expr)
  if ex.head == :$
    string("\$(", map(string, ex.args)..., ")")
  else 
    Base.string(ex)
  end
end 
end

However, if it executes the last command in the list, this somehow calls my 
string function rather than the Base string function, leading to an 
infinite recursion.

If I name the function differently and don't override Base.string, eg:

module M
function tostring(ex::Expr)
  if ex.head == :$
    string("\$(", map(tostring, ex.args)..., ")")
  else 
    string(ex)
  end
end 
end

then the string(ex) call never calls tostring again (for obvious reasons).

Which means that if I want to implement my own custom string function on 
expressions, I have to actually replicate the logic for EVERY type 
possible, which is a LOT of work. I'd rather not do this, and I was hoping 
to understand why I can't just exist as a new method definition that will 
work only on a specific type (expr) in this case, and do what would 
normally be right in other cases. 

Vishesh

Reply via email to