Julia 来自赋值的函数返回类型

Julia 来自赋值的函数返回类型,julia,Julia,这里我期望Int类型,但得到Float: julia> function test() i::Int = 3.0 end test (generic function with 1 method) julia> typeof(test()) Float64 在这种情况下,返回类型为Int: julia> function test() i::Int = 3.0 i end test (gene

这里我期望Int类型,但得到Float:

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

julia> typeof(test())
Float64
在这种情况下,返回类型为Int:

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

julia> typeof(test())
Int64

这是正确的行为还是错误

下面是杰夫的一句话:

因此在第一个示例中,它相当于直接返回
=
返回的内容,即
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))

抓住了,但不是虫子。很好的例子说明了为什么显式返回语句通常是个好主意!