gpt4 book ai didi

types - 如何解决函数签名中的 "UndefVarError: T not defined"

转载 作者:行者123 更新时间:2023-12-04 00:07:46 30 4
gpt4 key购买 nike

我正在尝试运行(其他人的代码),看起来像

function f{T<:Number}(n::Int, alpha::T, beta::T)
...
end

当“使用”该文件时,我得到
UndefVarError: T not defined
Stacktrace:
[1] top-level scope at [file location]:[line number of function definition above]

从我在文档( https://docs.julialang.org/en/v1/base/numbers/ )中读到的内容来看,上面的语法看起来是正确的。知道为什么我会收到此错误吗?

最佳答案

这是旧的 Julia 语法。此函数应重新编写如下,否则您将需要切换到 Julia 0.6 或之前的版本之一。

function f(n::Int, alpha::T, beta::T) where {T<:Number}
...
end

我在 Julia 0.7 上运行了以下内容,这是我得到的:
julia> function f{T<:Number}(n::Int, alpha::T, beta::T)
print("Test")
end
┌ Warning: Deprecated syntax `parametric method syntax f{T <: Number}(n::Int, alpha::T, beta::T)` around REPL[1]:2.
│ Use `f(n::Int, alpha::T, beta::T) where T <: Number` instead.
└ @ REPL[1]:2
f (generic function with 1 method)

Here is the link to the general syntax for the where keyword.

Here is the link to a similar StackOverflow post explaining what the where keyword does.

关于types - 如何解决函数签名中的 "UndefVarError: T not defined",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58253225/

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