Function return type from assignement

Here I was expecting an Int type, but got a Float:

julia> function test()
         i::Int = 3.0
       end
test (generic function with 1 method)

julia> typeof(test())
Float64

and in this case, the return type is Int:

julia> function test()
         i::Int = 3.0
         i
       end
test (generic function with 1 method)

julia> typeof(test())
Int64

Is this the correct behavior or error?

+4
source share
1 answer

Here is a quote from Jeff:

=returns the right side every time. There are no exceptions.

therefore, in the first example, this is equivalent to directly returning what it returns =, i.e. 3.0:

julia> @code_lowered test()
CodeInfo(:(begin 
        nothing
        SSAValue(0) = 3.0
        i = (Core.typeassert)((Base.convert)(Main.Int, SSAValue(0)), Main.Int)
        return SSAValue(0)
    end))
+6
source

Source: https://habr.com/ru/post/1687192/


All Articles