问题
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