# Help to build a macro to mimic the With of VBA

**URL:** <https://discourse.julialang.org/t/help-to-build-a-macro-to-mimic-the-with-of-vba/24116>\
**Category:** General Usage\
**Created:** [May 11, 2019, 6:22pm UTC](https://discourse.julialang.org/t/help-to-build-a-macro-to-mimic-the-with-of-vba/24116 "2019-05-11T18:22:55Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [May 11, 2019, 6:22pm UTC](https://discourse.julialang.org/t/help-to-build-a-macro-to-mimic-the-with-of-vba/24116/1 "2019-05-11T18:22:55Z")

</div>

Hi guys!

Sometimes, the `With` feature of VBA can be very useful. Consider something like:

```julia
    general.insert("simProgress", generalFrame.simProgress);
    general.insert("simulationDate", simulationDate);
    general.insert("a", generalFrame.a);
    general.insert("e", generalFrame.e);
    general.insert("i", generalFrame.i);
    general.insert("raan", generalFrame.raan);
    general.insert("w", generalFrame.w);
    general.insert("f", generalFrame.f);
    general.insert("lat", generalFrame.lat);
    general.insert("lon", generalFrame.lon);
    general.insert("h", generalFrame.h);
    general.insert("eclipse", generalFrame.eclipse == 0x01);
    general.insert("gmst", generalFrame.GMST);
    general.insert("sun_i", v_sun_i);

```

I would like to write a macro that I can do something like:

```julia
@with generalFrame
    general.insert("simProgress", .simProgress);
    general.insert("simulationDate", simulationDate);
    general.insert("a", .a);
    general.insert("e", .e);
    general.insert("i", .i);
    general.insert("raan", .raan);
    general.insert("w", .w);
    general.insert("f", .f);
    general.insert("lat", .lat);
    general.insert("lon", .lon);
    general.insert("h", .h);
    general.insert("eclipse", .eclipse == 0x01);
    general.insert("gmst", .GMST);
    general.insert("sun_i", v_sun_i);
end

```

I thought it was simple, every time I find an expression starting with `.`, then I replace with `general.`. However, `.lat`, for example, is not a valid Julia expression. Hence, I am stuck and now I need help 🙂

---

<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 11, 2019, 6:42pm UTC](https://discourse.julialang.org/t/help-to-build-a-macro-to-mimic-the-with-of-vba/24116/2 "2019-05-11T18:42:31Z")

</div>

I’m not entirely sure what the effect of the VBA code is… As Kristoffer often says if you want to write a macro start with the code that you /the user would write, and then the desired code that you would expect if the macro would work as intended.  
If you can produce an MWE along those lines I’m sure people here can help!

---

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [May 11, 2019, 6:50pm UTC](https://discourse.julialang.org/t/help-to-build-a-macro-to-mimic-the-with-of-vba/24116/3 "2019-05-11T18:50:27Z")

</div>

The effect I want to achieve is pretty simple. I want to have a macro to avoid typing the name of a structure over and over again. So, the very minimum example is to turn

```julia
mutable struct MyStructure
    a::Int64
    b::Int64
    c::Int64
end

mystruct = MyStructure(0,1,2)

@with mystruct begin
    .a = 1
    .b = 2
    .c = 3
end

```

into

```julia
mutable struct MyStructure
    a::Int64
    b::Int64
    c::Int64
end

mystruct = MyStructure(0,1,2)

mystruct.a = 1
mystruct.b = 2
mystruct.c = 3

```

---

<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:** [May 11, 2019, 7:53pm UTC](https://discourse.julialang.org/t/help-to-build-a-macro-to-mimic-the-with-of-vba/24116/4 "2019-05-11T19:53:00Z")

</div>

> [@Ronis\_BR](#):
>
> However, `.lat` , for example, is not a valid Julia expression. Hence, I am stuck and now I need help

Macros transform Julia syntax to other Julia syntax. If you don’t want the input to be Julia syntax then you need to use a string macro.

---

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [May 11, 2019, 8:02pm UTC](https://discourse.julialang.org/t/help-to-build-a-macro-to-mimic-the-with-of-vba/24116/5 "2019-05-11T20:02:01Z")

</div>

hum I see, thanks for the tip. The problem with the string macro is that the editors will show without the highlights. Maybe I need to find a good replacement literal.

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [May 11, 2019, 8:07pm UTC](https://discourse.julialang.org/t/help-to-build-a-macro-to-mimic-the-with-of-vba/24116/6 "2019-05-11T20:07:35Z")

</div>

Do g = generalFrame then use g.a etc.  
Only 1 more character to type, no macro required, and it’s clearer to read 😉

---

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [May 11, 2019, 8:14pm UTC](https://discourse.julialang.org/t/help-to-build-a-macro-to-mimic-the-with-of-vba/24116/7 "2019-05-11T20:14:07Z")

</div>

OK, you convinced me 🙂

---

<div class="post-metadata">

**Author:** ![IljaK91](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iljak91/32/44301_2.png) [@IljaK91](https://discourse.julialang.org/u/IljaK91)\
**Post date:** [May 11, 2019, 8:52pm UTC](https://discourse.julialang.org/t/help-to-build-a-macro-to-mimic-the-with-of-vba/24116/8 "2019-05-11T20:52:06Z")

</div>

I think for DataFrames something of the sort already exists

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [May 11, 2019, 9:59pm UTC](https://discourse.julialang.org/t/help-to-build-a-macro-to-mimic-the-with-of-vba/24116/9 "2019-05-11T21:59:02Z")

</div>

What type is general there? Feels like you should be able to automate that all with a dictionary or similar.

---

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [May 11, 2019, 10:41pm UTC](https://discourse.julialang.org/t/help-to-build-a-macro-to-mimic-the-with-of-vba/24116/10 "2019-05-11T22:41:49Z")

</div>

It is a structure with configuration parameters (it has something like 100 variables). I think it can be written using `fieldnames` but I am not sure if the effort is worthy. The parameters may or may not need a unit conversion.

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [May 12, 2019, 2:59am UTC](https://discourse.julialang.org/t/help-to-build-a-macro-to-mimic-the-with-of-vba/24116/11 "2019-05-12T02:59:04Z")

</div>

Does Parameters.jl help at all?

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [May 12, 2019, 8:27am UTC](https://discourse.julialang.org/t/help-to-build-a-macro-to-mimic-the-with-of-vba/24116/12 "2019-05-12T08:27:56Z")

</div>

You could use a specific pattern, eg an underscore

```julia
_simProgress

```

and walk the code using [MacroTools.jl](https://mikeinnes.github.io/MacroTools.jl/stable/pattern-matching/#Expression-Walking-1) to replace each symbol beginning with an underscore with the relevant expression. You would, of course, have to distinguish assignment, which could be tricky.

Generally, these patterns are more useful for interpreted languages, where the contents of a composite type can be appended to the list of variables at runtime — eg R has similar `with()` and `within()` macros. But for Julia, the suggestion by @dpsanders is much more idiomatic.

---

<div class="post-metadata">

**Author:** ![c42f](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/c42f/32/52842_2.png) [@c42f](https://discourse.julialang.org/u/c42f)\
**Post date:** [May 13, 2019, 6:02am UTC](https://discourse.julialang.org/t/help-to-build-a-macro-to-mimic-the-with-of-vba/24116/13 "2019-05-13T06:02:10Z")

</div>

This might be a nice use of a let block to keep the temporary names in a confined scope:

```julia
let g = general, gf = generalFrame
    g.insert("simProgress", gf.simProgress);
    g.insert("simulationDate", simulationDate);
    g.insert("a", gf.a);
    g.insert("e", gf.e);
    g.insert("i", gf.i);
    g.insert("raan", gf.raan);
    g.insert("w", gf.w);
    g.insert("f", gf.f);
    g.insert("lat", gf.lat);
    g.insert("lon", gf.lon);
    g.insert("h", gf.h);
    g.insert("eclipse", gf.eclipse == 0x01);
    g.insert("gmst", gf.GMST);
    g.insert("sun_i", v_sun_i);
end

```

---

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [May 13, 2019, 10:35am UTC](https://discourse.julialang.org/t/help-to-build-a-macro-to-mimic-the-with-of-vba/24116/14 "2019-05-13T10:35:52Z")

</div>

Sometimes, it seems an overkill to use Parameters.jl IMHO. But I do use it a lot.

---

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [May 13, 2019, 11:03am UTC](https://discourse.julialang.org/t/help-to-build-a-macro-to-mimic-the-with-of-vba/24116/15 "2019-05-13T11:03:14Z")

</div>

This is indeed awesome! Thanks!
