Well, you could do this by defining another method on the (more specific)
Integer type:

test(x::Real) = x*x
test(x::Integer) = error()

There's also the FloatingPoint type, but that excludes pi.

I have to say, though, that it seems odd that you'd want to do this, seeing
as integers are effectively a special case of floating point numbers.


On 17 May 2014 13:40, Hans W Borchers <[email protected]> wrote:

> Thanks, Mike, for the prompt answer.
> But what if i want to explicitly exclude integers.
> I think with Real I would allow them.
>
>
> On Saturday, May 17, 2014 2:13:09 PM UTC+2, Mike Innes wrote:
>>
>> I think your first example is right, although someone may well correct me
>> on that. That's how I've done similar things, anyway.
>>
>> As for your second example, this is happening because your type parameter
>> is overly restrictive – if you use ::Real it works as you would expect.
>>
>> Conversions like this will certainly take place where they make sense,
>> but they don't really make sense here – it has to be assumed that you've
>> asked for a Float64 because you specifically need one.
>>
>> – Mike
>>
>> On Saturday, 17 May 2014 12:59:02 UTC+1, Hans W Borchers wrote:
>>>
>>> Yesterday I implemented a function calculating arc length of curves (to
>>> the last digit) when I came across the following stumbling blocks. Image
>>> the following function where I leave a for-loop with a 'break' statement:
>>>
>>>     function testfun1(x::Vector{Float64})
>>>         for i = 1:length(x)
>>>             if x[i] == 0.0
>>>                 break
>>>             end
>>>         end
>>>         return i-1
>>>     end
>>>
>>>     julia> testfun([1.0, 2.0, 3.0, 0.0, 1.0])
>>>     ERROR: i not defined
>>>      in testfun at none:7
>>>
>>> I understand that the scope of the loop variable is restricted to the
>>> loop itself. What is the best way to "export"  i  to the outside? For
>>> the moment I settled with defining i before the loop.
>>>
>>>     function testfun2(x::Vector{Float64})
>>>         local i::Int
>>>         for i = 1:length(x)
>>>             if x[i] == 0.0
>>>                 break
>>>             end
>>>         end
>>>         return i-1
>>>     end
>>>
>>> This works, but I must admit it runs against my gut feeling and
>>> experience with other scientific programming languages.
>>>
>>>
>>> The next shock was the following:
>>>
>>>     function testfun(x::Float64)
>>>     return x^2
>>>     end
>>>
>>>     julia> testfun(pi)
>>>     ERROR: no method testfun(MathConst{:π})
>>>
>>> Again, I learned that I can use testfun(float(pi) , but my feeling
>>> would be that pi should be converted to float automatically whenever
>>> the context requires it. On the mailing list I think I have seen other
>>> complaints about this. I would prefer that  pi  and MathConst{:π} (or
>>> even better π alone) were different objects.
>>>
>>>

Reply via email to