# Check if list of variables are defined

**URL:** <https://discourse.julialang.org/t/check-if-list-of-variables-are-defined/81024>\
**Category:** General Usage\
**Created:** [May 13, 2022, 2:41pm UTC](https://discourse.julialang.org/t/check-if-list-of-variables-are-defined/81024 "2022-05-13T14:41:03Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jman87](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jman87/32/1423_2.png) [@jman87](https://discourse.julialang.org/u/jman87)\
**Post date:** [May 13, 2022, 2:41pm UTC](https://discourse.julialang.org/t/check-if-list-of-variables-are-defined/81024/1 "2022-05-13T14:41:03Z")

</div>

Hi all,

I’m trying to determine if a user has defined all the necessary input variables. I’d like to store the variable names in a list (either a tuple or array) and then be able to cycle through it and check if they’re defined. This seems like it should be simple, but I can’t get the syntax right. I tried various things such as the following, but I get errors like the one below. Any suggestions?

```julia
julia> a = 1
1

julia> vlist = (:a, :b)
(:a, :b)

julia> @isdefined(vlist[1])
ERROR: LoadError: MethodError: no method matching var"@isdefined"(::LineNumberNode, ::Module, ::Expr)
Closest candidates are:
  var"@isdefined"(::LineNumberNode, ::Module, ::Symbol) at /Applications/Julia-1.7.app/Contents/Resources/julia/share/julia/base/essentials.jl:150
in expression starting at REPL[58]:1

```

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [May 13, 2022, 2:45pm UTC](https://discourse.julialang.org/t/check-if-list-of-variables-are-defined/81024/2 "2022-05-13T14:45:41Z")

</div>

```julia
julia> isdefined.(Ref(Main), vlist)
(true, false)

```

---

<div class="post-metadata">

**Author:** ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)\
**Post date:** [May 13, 2022, 2:46pm UTC](https://discourse.julialang.org/t/check-if-list-of-variables-are-defined/81024/3 "2022-05-13T14:46:47Z")

</div>

You can use `isdefined(@ __MODULE__ , vlist[1])` instead. I’d encourage you to find a different way to structure your code though; this pattern isn’t (usually) a good idea.

---

<div class="post-metadata">

**Author:** ![jman87](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jman87/32/1423_2.png) [@jman87](https://discourse.julialang.org/u/jman87)\
**Post date:** [May 13, 2022, 3:33pm UTC](https://discourse.julialang.org/t/check-if-list-of-variables-are-defined/81024/4 "2022-05-13T15:33:49Z")

</div>

So if I do what @nilshg suggested I get the right results, but I get the following with what @pfitzseb suggested, which seems weird as this is pretty much the same thing:

```julia
julia> a
1

julia> b
2

julia> c
ERROR: UndefVarError: c not defined

julia> d
ERROR: UndefVarError: d not defined

julia> isdefined.(Ref(Main), vlist)
(true, true, false, false)

julia> isdefined(Ref(Main), vlist[1])
false

julia> isdefined(Ref(Main), vlist[2])
false

julia> isdefined(Ref(Main), vlist[3])
false

julia> isdefined(Ref(Main), vlist[4])
false

```

---

<div class="post-metadata">

**Author:** ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)\
**Post date:** [May 13, 2022, 3:34pm UTC](https://discourse.julialang.org/t/check-if-list-of-variables-are-defined/81024/5 "2022-05-13T15:34:51Z")

</div>

That’s not what I wrote though. You have an extraneous `Ref` in there.

---

<div class="post-metadata">

**Author:** ![jman87](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jman87/32/1423_2.png) [@jman87](https://discourse.julialang.org/u/jman87)\
**Post date:** [May 13, 2022, 3:38pm UTC](https://discourse.julialang.org/t/check-if-list-of-variables-are-defined/81024/6 "2022-05-13T15:38:33Z")

</div>

My apologies… was thinking those would both be pointing to the same thing for the example in the REPL. Yes, they both work fine.

@pfitzseb what exactly is the @ **MODULE** doing?

---

<div class="post-metadata">

**Author:** ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)\
**Post date:** [May 13, 2022, 3:41pm UTC](https://discourse.julialang.org/t/check-if-list-of-variables-are-defined/81024/7 "2022-05-13T15:41:01Z")

</div>

```julia
help?> @ __MODULE__
  @ __MODULE__ -> Module

  Get the Module of the toplevel eval, which is the Module code is currently
  being read from.

```

Generally, `Ref(x) != x`; when broadcasting, the `Ref` “unwrapped”.
