Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Julia no automatic type conversion inside function

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?

like image 814
max Avatar asked Nov 11 '22 03:11

max


1 Answers

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.

like image 153
Chris Rackauckas Avatar answered Dec 07 '22 00:12

Chris Rackauckas