esel
esel

Reputation: 983

Julia: Use operator that is not in Main module

What is the best way to use an operator as a function in Julia (1.x) without defining it in the Main module? E. g. if I would like to use the (latex: \cdot) operator from the LinearAlgebra module without adding the operator in the Main module?

The naive solution gives an error:

julia> import LinearAlgebra
julia> LinearAlgebra.⋅(1,2)
ERROR: UndefVarError: ⋅ not defined

The only solution that came to my mind is:

import LinearAlgebra
op = LinearAlgebra.eval(Meta.parse("⋅")) # get the function object
op(1,2) # use it

Is there a better way than this?

Upvotes: 1

Views: 138

Answers (1)

lungben
lungben

Reputation: 1158

I assume that you do not want to just call

using LinearAlgebra

to bring the dot operator into current module namespace, right?

In this case, you can call operators in a module by prepending the operator with a colon:

import LinearAlgebra
LinearAlgebra.:⋅((1,2), (0,1))

Upvotes: 4

Related Questions