I even tried below code, but it still went into an infinite recursion:
foo2=copy(foo)
function foo(X::Int)
    @assert(X>=0,"X should be a positive number.")
    foo2(X)
end
Since the copy or deepcopy can not really copy a function but still just be 
a reference of it.


On Sunday, April 26, 2015 at 10:22:34 AM UTC+2, Jerry Xiong wrote:
>
> Yes. I tried it, and it indeed went into an infinite recursion.
>
> On Sunday, April 26, 2015 at 10:06:57 AM UTC+2, Mauro wrote:
>>
>> > Thank you! For this question, invoke indeed a good solution :) 
>> > 
>> > How about a more general case. For example, I already have a function 
>> foo 
>> > foo(X::Int)=X+1 
>> > in the environment. 
>> > 
>> > Then I want to overload foo to forbid negative input: 
>> > function foo(X::Int) 
>> >     @assert(X>=0,"X should be a positive number.") 
>> >     invoke(foo,(Int,),X)#Here, I hope to call the original definition 
>> of 
>> > foo. 
>> > end 
>> > 
>> > However, invoke doesn't work as I expected in this case. Is there any 
>> other 
>> > solution? 
>>
>> I don't think there is.  There can only be one method for each signature 
>> for one generic function.  So above gets you into an infinite recursion. 
>>
>> > On Sunday, April 26, 2015 at 8:40:06 AM UTC+2, Sam L wrote: 
>> >> 
>> >> See ?invoke. 
>> >> 
>> >> display(X::Vector)=length(X)>10?print("Too long to show."): 
>> >> invoke(display, (Any,), X) 
>> >> 
>> >> On Saturday, April 25, 2015 at 10:41:39 PM UTC-7, Jerry Xiong wrote: 
>> >>> 
>> >>> For example, if I want to overload the Base.display(::Vector) to 
>> repress 
>> >>> the display when the vector is too long, I coded as below: 
>> >>> julia> import Base.display 
>> >>> 
>> >>> julia> display(X::Vector)=length(X)>10?print("Too long to 
>> show."):Base. 
>> >>> display(X) 
>> >>> display (generic function with 17 methods) 
>> >>> 
>> >>> julia> display([1,2,3]) 
>> >>> ERROR: stack overflow 
>> >>>  in display at none:1 (repeats 39998 times) 
>> >>> 
>> >>> I want to call the original Base.display when the length of vector is 
>> >>> less than 10, but it is became a dead recurring. Is there any way to 
>> do it? 
>> >>> 
>> >> 
>>
>>

Reply via email to