gpt4 book ai didi

julia - Julia函式 header 中的无名值是什么意思?

转载 作者:行者123 更新时间:2023-12-04 13:47:55 25 4
gpt4 key购买 nike

我经常在Julia中看到类似以下内容的东西:

convert(::Type{Point{Float64}}, ::Float64)
(::如何工作?那么这是什么术语呢?

最佳答案

您的答案可以在the Julia documentation for defining conversions中找到。引用(将类型切换为使其更易于阅读):

The type of the first argument of this method is a singleton type, Type{Point{Float64}}, the only instance of which is Point{Float64}. Thus, this method is only invoked when the first argument is the type value Point{Float64}. Notice the syntax used for the first argument: the argument name is omitted prior to the :: symbol, and only the type is given. This is the syntax in Julia for a function argument whose type is specified but whose value is never used in the function body. In this example, since the type is a singleton, there would never be any reason to use its value within the body.



(强调我的)

当尝试使用错误类型的参数调用函数时,您还将在错误消息中遇到 foo(::SomeType)语法(毕竟,您无法显示不存在的变量的参数名称)。例如:
julia> foo(x::Bool) = 3
foo (generic function with 1 method)

julia> foo(5)
ERROR: `foo` has no method matching foo(::Int64)

关于julia - Julia函式 header 中的无名值是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37039016/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com