Destructuring function definition?

Can you help me understand

julia> module A
           f() = g()
           g() = f()
       end
Main.A

julia> module B
           f(), g() = g(), f()
       end
ERROR: UndefVarError: `g` not defined in `Main.B`

?

Oh, I see. the line inside B is not function definition.

julia> module C
           f() = rand()
       end
Main.C

julia> C.f()
0.24515636911767225

julia> C.f()
0.6758273362892651

julia> module D
           f(), g() = rand(), rand()
       end
Main.D

julia> D.f()
0.6869168702705465

julia> D.f()
0.6869168702705465

julia> D.f
f (generic function with 1 method)

The problem in the original post was not related to the line in B being or not a function definition, but to g (referred to in the right hand side of that line) having been defined in a module A that B cannot “see”. In your second example, on the other hand, the right hand side of the line in D refers to functions that are in Main (rand), so they can be seen.

By the way, what I didn’t expect is that defining a tuple of functions does not behave as when they are defined individually: the function calls in the “definition” are evaluated right away, so rand is not random anymore. This can be seen more clearly if those definitions are made outside a module, directly on Main:

julia> f() = rand()
f (generic function with 1 method)

julia> g(), h() = rand(), rand()
(0.7979213943713473, 0.6634292395166483)
1 Like