分配的函数返回类型

光滑

在这里,我期望使用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))

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章