# @track things. HOWTO: Is it possible to create global scope variable from local scope (function)

**URL:** https://discourse.julialang.org/t/track-things-howto-is-it-possible-to-create-global-scope-variable-from-local-scope-function/76814
**Category:** General Usage
**Tags:** question, metaprogramming
**Created:** [February 20, 2022, 7:07pm UTC](https://discourse.julialang.org/t/track-things-howto-is-it-possible-to-create-global-scope-variable-from-local-scope-function/76814 "2022-02-20T19:07:57Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Marcell\_Havlik](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marcell_havlik/32/37424_2.png) [@Marcell\_Havlik](https://discourse.julialang.org/u/Marcell_Havlik)
#### Post date: [February 20, 2022, 7:07pm UTC](https://discourse.julialang.org/t/track-things-howto-is-it-possible-to-create-global-scope-variable-from-local-scope-function/76814/1 "2022-02-20T19:07:57Z")

</div>

Hey Julianners,

I want something like this:

```julia
function fn(m) 
  # very compley function... and "m" would be a complex shit... that is hard to print
  @track modelsss m[1]=4+9 # mimicing a possible case
  # ex.: using m[1] later on 
end
c=[3,4]
fn(c)
modelsss

```

I have this @track macro:

```julia
macro track(var, ex)
	esc(quote
			((@isdefined $var) == false) && ($var = []); # create the variable... but it is only LOCAL scope :(
			res = $ex
			push!($var, res)
	end)
end

```

But this create a local variable.  
Is this even possible? Or I have to have a evasion maneuver like creating the variable outside and calling global beforehand ☹

Bests!  
Marcell

---

<div class="post-metadata">

### Author: ![Marcell\_Havlik](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marcell_havlik/32/37424_2.png) [@Marcell\_Havlik](https://discourse.julialang.org/u/Marcell_Havlik)
#### Post date: [February 20, 2022, 7:12pm UTC](https://discourse.julialang.org/t/track-things-howto-is-it-possible-to-create-global-scope-variable-from-local-scope-function/76814/2 "2022-02-20T19:12:05Z")

</div>

Wow I think I solved…

```julia
macro track(var, ex)
	esc(quote
			((@isdefined $var) == false) && (global $var = []);
			res = $ex
			push!($var, res)
	end)
end

```

I didn’t know if this is this easy… :o Julia constinously suprise me!

Anyway I hope it is a useful macro for everyone! 😉

---

<div class="post-metadata">

### Author: ![Marcell\_Havlik](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marcell_havlik/32/37424_2.png) [@Marcell\_Havlik](https://discourse.julialang.org/u/Marcell_Havlik)
#### Post date: [February 20, 2022, 7:17pm UTC](https://discourse.julialang.org/t/track-things-howto-is-it-possible-to-create-global-scope-variable-from-local-scope-function/76814/3 "2022-02-20T19:17:12Z")

</div>

One more improvement, so we don’t run our expression’s return:

```julia
macro track(var, ex)
	esc(quote
			((@isdefined $var) == false) && (global $var = []);
			res = $ex
			push!($var, res)
			res
	end)
end

```

---

<div class="post-metadata">

### Author: ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)
#### Post date: [February 20, 2022, 8:26pm UTC](https://discourse.julialang.org/t/track-things-howto-is-it-possible-to-create-global-scope-variable-from-local-scope-function/76814/4 "2022-02-20T20:26:38Z")

</div>

You’ll want to selectively escape `var` and `ex` rather than the whole block.

---

<div class="post-metadata">

### Author: ![Marcell\_Havlik](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marcell_havlik/32/37424_2.png) [@Marcell\_Havlik](https://discourse.julialang.org/u/Marcell_Havlik)
#### Post date: [February 20, 2022, 8:51pm UTC](https://discourse.julialang.org/t/track-things-howto-is-it-possible-to-create-global-scope-variable-from-local-scope-function/76814/5 "2022-02-20T20:51:53Z")

</div>

What is the difference in the behind?

I lack too much knowledge to implement this on the macro field in spite I tried to understand a year before:

```julia
macro track(vari, ex)
	eva = esc(vari)
	eex = esc(ex)
	quote
		((@isdefined $eva) == false) && (global $eva = []);
		res = $eex
		push!($eva, res)
		res
	end
end

```

This is somehow terribly wrong. And errors everywhere… 😃  
I used escaping too long time ago and just cannot remeber how to do it… 😕

---

<div class="post-metadata">

### Author: ![tk3369](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tk3369/32/2824_2.png) [@tk3369](https://discourse.julialang.org/u/tk3369)
#### Post date: [February 21, 2022, 4:33am UTC](https://discourse.julialang.org/t/track-things-howto-is-it-possible-to-create-global-scope-variable-from-local-scope-function/76814/6 "2022-02-21T04:33:22Z")

</div>

My video could be helpful here 🙂

[![](https://global.discourse-cdn.com/julialang/original/3X/2/7/27e4fbeabd079d7f8b36b0626a49f6488ea40a1d.jpeg "Julia Macro hygiene made easy! | Tom Kwong") ](https://www.youtube.com/watch?v=JePBb9-ychE)

---

<div class="post-metadata">

### Author: ![Marcell\_Havlik](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marcell_havlik/32/37424_2.png) [@Marcell\_Havlik](https://discourse.julialang.org/u/Marcell_Havlik)
#### Post date: [February 21, 2022, 7:47am UTC](https://discourse.julialang.org/t/track-things-howto-is-it-possible-to-create-global-scope-variable-from-local-scope-function/76814/7 "2022-02-21T07:47:43Z")

</div>

I stuck with a "module scope variable @isdefined checkin sadly… :

```julia
macro track(vari, ex)
	eva = esc(vari)
	eex = esc(ex)
	quote
		esc(((@isdefined $((vari))) == false) && (global $vari = []));
		res = $eex
		push!($eva, res)
		res
	end
end

```

I don’t know how to check the “vari” in the “Main.” scope I think.

---

<div class="post-metadata">

### Author: ![Marcell\_Havlik](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marcell_havlik/32/37424_2.png) [@Marcell\_Havlik](https://discourse.julialang.org/u/Marcell_Havlik)
#### Post date: [February 21, 2022, 7:53am UTC](https://discourse.julialang.org/t/track-things-howto-is-it-possible-to-create-global-scope-variable-from-local-scope-function/76814/8 "2022-02-21T07:53:45Z")

</div>

Ok, I think I do this and at least I make a little macro higiene:

```julia
macro track(var, ex)
	res = gensym()
	esc(quote
			((@isdefined $var) == false) && (global $var = []);
			$res = $ex
			push!($var, $res)
			$res
	end)
end

```
