# Error when using @load after if statement

**URL:** <https://discourse.julialang.org/t/error-when-using-load-after-if-statement/117625>\
**Category:** General Usage\
**Tags:** question\
**Created:** [July 30, 2024, 8:05am UTC](https://discourse.julialang.org/t/error-when-using-load-after-if-statement/117625 "2024-07-30T08:05:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![J\_B\_R](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/j_b_r/32/50665_2.png) [@J\_B\_R](https://discourse.julialang.org/u/J_B_R)\
**Post date:** [July 30, 2024, 8:05am UTC](https://discourse.julialang.org/t/error-when-using-load-after-if-statement/117625/1 "2024-07-30T08:05:07Z")

</div>

Hi there – I would like to write a code that loads all data of a file, but only in case this file exists. I use the @load function of JLD2. However, integrating @load into an if statement leads to an error if the file does not exist. Does anyone have a suggestion to get around this? Here is a code example:

```julia
using JLD2

if isfile("TEST_FILE.jld2")
    @load("TEST_FILE.jld2")
end

```

The error does not appear if `jldopen` is used, but @load works much better for my needs. Whatever the reason is, I am interested to load all data from a file, if the file exists (Breakpointfile from a long-running code).  
Many thanks! Jonas

---

<div class="post-metadata">

**Author:** ![eldee](https://avatars.discourse-cdn.com/v4/letter/e/b5a626/32.png) [@eldee](https://discourse.julialang.org/u/eldee)\
**Post date:** [July 31, 2024, 5:02pm UTC](https://discourse.julialang.org/t/error-when-using-load-after-if-statement/117625/2 "2024-07-31T17:02:10Z")

</div>

Hi Jonas,

The problem with `@load` is that the opening of the file does not occur in the returned Expression, but in the body of the macro itself. Therefore, the file will be opened at parse-time, regardless of the `if`-statement.

> **@load implementation**
>
> ```julia
> julia> @less @load("TEST_FILE.jld2")
> 
> macro load(filename, vars...)
> if isempty(vars)
> if isa(filename, Expr)
> throw(ArgumentError("filename argument must be a string literal unless variable names are specified"))
> end
> # Load all variables in the top level of the file
> vars = Symbol[]
> f = jldopen(filename)
> try
> for n in keys(f)
> if !isgroup(f, lookup_offset(f.root_group, n))
> push!(vars, Symbol(n))
> end
> end
> finally
> close(f)
> end
> end
> return quote
> ($([esc(x) for x in vars]...),) = jldopen($(esc(filename))) do f
> ($([:(read(f, $(string(x)))) for x in vars]...),)
> end
> $(Symbol[v for v in vars]) # convert to Array
> end
> end
> 
> ```

Before continuing, let me first point out that `@load` and `@save` are listed under _Legacy_ in the [documentation](https://juliaio.github.io/JLD2.jl/dev/legacy/), and they mention that _“Over time a range of issues have been opened by new users struggling with them.”_ In other words, it’s probably best to refactor your code to avoid `@load`.

That being said, you could easily ‘fix’ `@load` by adding your `isfile(filename)` check before the `f = jldopen(filename)` line, e.g. as `isfile(filename) || return`. (Note that you’ll have to `import JLD2.lookup_offset, JLD2.isgroup`.) Alternatively, you can move the `jldopen` to inside the `quote`. Or if you want a quick-and-dirty hack, you can delay the macro parsing, e.g. via

```julia
julia> if isfile("non_existing_file.jld2")
           eval(:(@load("non_existing_file.jld2")))
       end

julia> if isfile("existing_file.jld2")
          eval(:(@load("existing_file.jld2")))
       end
2-element Vector{Symbol}:
 :hello
 :xs

```

---

<div class="post-metadata">

**Author:** ![J\_B\_R](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/j_b_r/32/50665_2.png) [@J\_B\_R](https://discourse.julialang.org/u/J_B_R)\
**Post date:** [August 1, 2024, 7:28am UTC](https://discourse.julialang.org/t/error-when-using-load-after-if-statement/117625/3 "2024-08-01T07:28:34Z")

</div>

Dear eldee  
Many thanks for the detailed explanation! It fixed the error and furthermore I learned a lot.  
Best wishes
