如何解决"UndefVarError:未定义".在功能签名中 [英] How to resolve "UndefVarError: T not defined" in function signature
问题描述
我正在尝试运行看起来像其他人的代码
I am trying to run (someone else's code) that looks like
function f{T<:Number}(n::Int, alpha::T, beta::T)
...
end
使用"该文件时,我得到
When "using" that file I get
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/),那么上面的语法似乎是正确的.知道为什么我会收到此错误吗?
From what I've read in the documentation (https://docs.julialang.org/en/v1/base/numbers/), it looks like the syntax above is correct. Any idea why I'm getting this error?
推荐答案
这是古老的Julia语法.该功能应重写如下,否则您将需要切换到Julia 0.6或之前的版本之一.
This is old Julia syntax. This function should be re-written as follows, or you will need to switch to Julia 0.6 or one of the versions that came before it.
function f(n::Int, alpha::T, beta::T) where {T<:Number}
...
end
我在Julia 0.7上运行了以下代码,这就是我得到的:
I ran the following on Julia 0.7 and here is what I got:
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.
此处是指向类似StackOverflow帖子的链接,该帖子解释了where
关键字的作用.
Here is the link to a similar StackOverflow post explaining what the where
keyword does.
这篇关于如何解决"UndefVarError:未定义".在功能签名中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!