# Eval of an expression containing :using

**URL:** <https://discourse.julialang.org/t/eval-of-an-expression-containing-using/23644>\
**Category:** New to Julia\
**Created:** [April 29, 2019, 10:54am UTC](https://discourse.julialang.org/t/eval-of-an-expression-containing-using/23644 "2019-04-29T10:54:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![leejm516](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leejm516/32/8219_2.png) [@leejm516](https://discourse.julialang.org/u/leejm516)\
**Post date:** [April 29, 2019, 10:54am UTC](https://discourse.julialang.org/t/eval-of-an-expression-containing-using/23644/1 "2019-04-29T10:54:56Z")

</div>

I have got a weird result with an expression containing `:using`.  
Let me show you an example.

```julia
julia> expression = :(using Gurobi)
:(using Gurobi)

julia> expression2 = Expr(:using, "Gurobi")
:(using Gurobi)

```

Both expressions appear to be same. But only `eval(expression)` worked.

```julia
julia> eval(expression)
ERROR: ArgumentError: Package Gurobi not found in current path:
- Run `import Pkg; Pkg.add("Gurobi")` to install the Gurobi package.

Stacktrace:
 [1] require(::Module, ::Symbol) at .\loading.jl:823
 [2] eval at .\boot.jl:328 [inlined]
 [3] eval(::Expr) at .\client.jl:404
 [4] top-level scope at none:0

julia> eval(expression2)

julia>

```

What was wrong with `expression2`?

Edit: the version of Julia is 1.1.0

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [April 29, 2019, 11:05am UTC](https://discourse.julialang.org/t/eval-of-an-expression-containing-using/23644/2 "2019-04-29T11:05:11Z")

</div>

You can use `dump` to find out.

Symbols in the expressions are (almost?) always `Symbol`s and not `String`s.

---

<div class="post-metadata">

**Author:** ![leejm516](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leejm516/32/8219_2.png) [@leejm516](https://discourse.julialang.org/u/leejm516)\
**Post date:** [April 29, 2019, 11:34am UTC](https://discourse.julialang.org/t/eval-of-an-expression-containing-using/23644/3 "2019-04-29T11:34:43Z")

</div>

I found a difference between `expression` and `expression2`.  
I read the manual and saw `dump`… but maybe I didn’t really understand its uses 😅

```julia
julia> dump(expression)
Expr
  head: Symbol using
  args: Array{Any}((1,))
    1: Expr
      head: Symbol .
      args: Array{Any}((1,))
        1: Symbol Gurobi

julia> dump(expression2)
Expr
  head: Symbol using
  args: Array{Any}((1,))
    1: String "Gurobi"

```

And I managed to think a solution out. The expression does not look nice, though.

```julia
julia> expression3 = Expr(:using, Expr(:., Symbol("Gurobi")))
:(using Gurobi)

julia> dump(expression3)
Expr
  head: Symbol using
  args: Array{Any}((1,))
    1: Expr
      head: Symbol .
      args: Array{Any}((1,))
        1: Symbol Gurobi

```

Any simpler suggestions?

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [April 29, 2019, 11:57am UTC](https://discourse.julialang.org/t/eval-of-an-expression-containing-using/23644/4 "2019-04-29T11:57:14Z")

</div>

1. Symbols can be constructed directly. `:Gurobi`.

2. Are you sure you didn’t change `expression` to `:(using .Gurobi)` instead?

3. It depends on what exactly are you trying to do. If you are looking for a way to not use any expression literal, then, well, this is what you are asking for and it’s as simple as it gets. If you are looking for a way to construct an expression “dynamically”, then you can just use interpolation, `:(using $name)`. You still need `Symbol` and if it’s a user supplied argument you should ask for `Symbol` directly.

---

<div class="post-metadata">

**Author:** ![leejm516](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leejm516/32/8219_2.png) [@leejm516](https://discourse.julialang.org/u/leejm516)\
**Post date:** [April 29, 2019, 12:38pm UTC](https://discourse.julialang.org/t/eval-of-an-expression-containing-using/23644/5 "2019-04-29T12:38:54Z")

</div>

What I want to do is to check whether a package is installed or not. An array of symbols is given, and the task is performed with a `for` loop. `:(using $name)` works nicely with my purpose 🙂.

It is still difficult for me to understand concepts of metaprogramming in Julia. Your comments were really helpful to get the feel of them, although lots of them have already been explained in Docs.
