But the idea is that if there is an expression like:

:(function(x) :(2 $x) end)

this looks like

(:function, (:call, :*, 2, (:$, :x)))

Now, when I do string() of this expression, i'd like it to use my 
definition of string when it matches the head of some expressions. 
Basically, if I call Base.string, it will never call my function back on 
nested calls.



On Wednesday, March 23, 2016 at 12:41:05 AM UTC-7, Eric Forgy wrote:
>
> Hi Vishesh,
>
> I'm not an expert, but one thought that comes to mind is to* not* import 
> Base.string. Then you can just define a "string" function local to your 
> module and then call Base.string, when you want to use the Base Julia 
> "string". By importing Base.string and then defining string, you are 
> changing the nature of Base Julia "string", which doesn't sound like a good 
> idea to me.
>
> Hope this helps.
>
> Eric
>
> On Wednesday, March 23, 2016 at 11:09:04 AM UTC+8, [email protected] 
> wrote:
>>
>> 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