Garbage-collect intermediate module variables

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

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:

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

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

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

1 Like

You can use a variation of what you wrote as:

let a1 = 3, a2 = 5
    global const var1 = a1 + a2
end
2 Likes

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

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.