How to resolve “UndefVarError: T not defined” in function signature

限于喜欢 提交于 2021-01-22 12:36:56

问题


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]

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?


回答1:


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

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.

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



来源:https://stackoverflow.com/questions/58253225/how-to-resolve-undefvarerror-t-not-defined-in-function-signature

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!