如何解决"UndefVarError:未定义".在功能签名中 [英] How to resolve "UndefVarError: T not defined" in function signature

查看:259
本文介绍了如何解决"UndefVarError:未定义".在功能签名中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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)

此处是where关键字的常规语法的链接.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆