# Garbage-collect intermediate module variables

**URL:** <https://discourse.julialang.org/t/garbage-collect-intermediate-module-variables/82747>\
**Category:** General Usage\
**Created:** [June 14, 2022, 2:04pm UTC](https://discourse.julialang.org/t/garbage-collect-intermediate-module-variables/82747 "2022-06-14T14:04:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ryofurue](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ryofurue/32/24531_2.png) [@ryofurue](https://discourse.julialang.org/u/ryofurue)\
**Post date:** [June 14, 2022, 2:04pm UTC](https://discourse.julialang.org/t/garbage-collect-intermediate-module-variables/82747/1 "2022-06-14T14:04:58Z")

</div>

I often want to export some values (variables) from a module and am wondering how to delete intermediate variables. As an illustration:

```julia
module somemod
   export var1, var2, . . .
   a1 = . . . # intermediate variables
   a2 = . . . #intermediate variables
   # . . .
   const var1 = . . . calculation over a1, a2, . . . 
   const var2 = . . . calculation over a1, a2, . . .
end

```

As you can see, the variables `a1`, `a2`, . . . can be discarded once the exported values have been calculated. What’s the standard/idiomatic way to discard these intermediate variables?

Of the top of my head, I think we can do something like this:

```julia
(var1, var2, . . . ) = 
let
   a1 = . . .
   a2 = . . .
   # . . . 
   var1 = . . .
   var2 = . . . 
   # . . .
   (var1, var2, . . .)
end

```

which is fine but looks too much contrived (unnatural) to me.

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [June 14, 2022, 2:10pm UTC](https://discourse.julialang.org/t/garbage-collect-intermediate-module-variables/82747/2 "2022-06-14T14:10:49Z")

</div>

> [@ryofurue](#):
>
> What’s the standard/idiomatic way to discard these intermediate variables?

Write a function that computes the things you want to export and call it when assigning your exports.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [June 14, 2022, 2:23pm UTC](https://discourse.julialang.org/t/garbage-collect-intermediate-module-variables/82747/3 "2022-06-14T14:23:56Z")

</div>

You can use a variation of what you wrote as:

```julia
let a1 = 3, a2 = 5
    global const var1 = a1 + a2
end

```

---

<div class="post-metadata">

**Author:** ![ryofurue](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ryofurue/32/24531_2.png) [@ryofurue](https://discourse.julialang.org/u/ryofurue)\
**Post date:** [June 15, 2022, 6:53am UTC](https://discourse.julialang.org/t/garbage-collect-intermediate-module-variables/82747/4 "2022-06-15T06:53:23Z")

</div>

> global const . . .

Thank you! That’s exactly the kind of solution I’ve been looking for.

I didn’t know that `global` in a local scape can **create** a global variable. My initial idea was

```julia
var1 = 1.0 # dummy value
var2 = 1.0 # dummy value
let a1 = . . . , a2 = . . .
   global var1 = . . . # overwrite the dummy value
   global var2 = . . . # overwrite the dummy value
end

```

but I thought it a bit silly.
