来自assignement的函数返回类型

Lis*_*iso 4 julia

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

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

julia> typeof(test())
Float64
Run Code Online (Sandbox Code Playgroud)

在这种情况下,返回类型是Int:

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

julia> typeof(test())
Int64
Run Code Online (Sandbox Code Playgroud)

这是正确的行为还是错误?

Gni*_*muc 6

以下是杰夫的一句话:

=每次都返回右侧.没有例外.

所以在第一个例子中,它相当于直接返回返回的内容,=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))
Run Code Online (Sandbox Code Playgroud)