EDIT:
This was just a bug that has since been fixed. I updated to Julia Version 0.3.0-rc1+32 and the code ran with no error.
https://github.com/JuliaLang/julia/issues/7830#event-148849792
Julia automatic type conversion from Int to Float does not work inside functions. Is there an easy way to make it work?
My question will be more clear with some code:
type A
a::FloatingPoint
end
instance = A(1)
#println("$instance") -> A(1.0)
#FINE
instance.a = 2
#println("$instance") -> A(2.0)
#THROWS ERROR
function fn()
instance.a = 3
end
fn()
The error is just a conversion error.
ERROR: type: setfield!: expected FloatingPoint, got Int64
in fn at /home/john/Desktop/test.jl:18
in include at ./boot.jl:245
in include_from_node1 at loading.jl:128
in process_options at ./client.jl:285
in _start at ./client.jl:354
while loading /home/john/Desktop/test.jl, in expression starting on line 20
I know that I can just add a decimal point to the end of the digit (i.e. "3.") in order to convert it to a float before assignment. However, the project that I am working on would be much easier if automatic conversion just worked inside functions.
What am I doing wrong?
The fix for this problem is to use a Julia release >=v0.3. Note that some of the syntax is the OP is deprecated in v0.4 and thus does not apply to newer versions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With