Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resolve "UndefVarError: T not defined" in function signature

Tags:

types

julia

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?

like image 247
notabot Avatar asked Oct 06 '19 00:10

notabot


1 Answers

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.

like image 120
logankilpatrick Avatar answered Sep 22 '22 02:09

logankilpatrick