Skip to content

Julia no automatic type conversion inside function #7830

@zarzle

Description

@zarzle

Julia automatic type conversion from Int to Float does not work inside functions. Is this a bug?

The issue is 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 of the assignment inside the function 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.

Is this a bug? Am I doing something wrong?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions