UndefVarError: * not defined

Viewed 1239

I'm having a persistent error where Julia tells me that the multiplication operator is undefined when I use multiplication within a package I'm working on. Nowhere in the package do I redefine or expand the Base multiplication operator, and there are some places where multiplication is recognized and others where it is not. For example, in the following code

    Λ = h.metric.Λ # Type Int64
    kSize = 2*Λ+1
    for i = 1:n_steps
        # Tempering
        r = temper(lf, r, (i=i, is_half=true), n_steps)
        # Take a half leapfrog step for momentum variable
        r = r - map(gradi -> ϵ / 2 .* gradi, grad)

the multiplication on line 2 gives me UndefVarError: * not defined wheras the multiplication on the last line compiles and executes without the error.

Multiplication outside the package and in the Julia REPL on Visual Studio Code does not seem to have the same error.

I've tried Using Base: * and import Base.* in both the main module and the individual Julia packages in the file to no avail. Any ideas about what could be going on?

2 Answers

This will appear if you accidentally assign to * later in the function or in a branch that didn't get taken. An easy place where this could happen is in an if expression that intends to ask if a*b == c but mistakenly uses = instead of ==:

julia> function test(a, b, c)
           d = a*b
           if (a*b = c)
               println("got $d")
           end
       end
test (generic function with 1 method)

julia> test(2,3,6)
ERROR: UndefVarError: * not defined
Stacktrace:
 [1] test(a::Int64, b::Int64, c::Int64)
   @ Main ./REPL[4]:2
 [2] top-level scope
   @ REPL[5]:1

Without a reproducible example, and full error message it is difficult to help. The only context that I can think of in which * would be undefined is within baremodule

julia> *
* (generic function with 328 methods)

julia> module M1
       *
       end
Main.M1

julia> baremodule M2
       *
       end
ERROR: UndefVarError: * not defined
Stacktrace:
 [1] top-level scope
   @ REPL[2]:2
Related