如何解决“UndefVarError: T 未定义"在函数签名中 [英] 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
当我使用"那个文件时
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)
这里是指向类似 StackOverflow 帖子的链接,该帖子解释了 where
关键字的作用.
Here is the link to a similar StackOverflow post explaining what the where
keyword does.
这篇关于如何解决“UndefVarError: T 未定义"在函数签名中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!