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.