# Let-in macro

**URL:** <https://discourse.julialang.org/t/let-in-macro/85431>\
**Category:** General Usage\
**Tags:** macros, syntax\
**Created:** [August 7, 2022, 7:57am UTC](https://discourse.julialang.org/t/let-in-macro/85431 "2022-08-07T07:57:27Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [August 7, 2022, 7:57am UTC](https://discourse.julialang.org/t/let-in-macro/85431/1 "2022-08-07T07:57:27Z")

</div>

I want a macro like this:

```julia
@let begin
a = 1
b = 2
in
y = a + b
z = a - b
end

```

defines in the containing scope `y` and `z` in terms of `a` and `b`, without defining `a` and `b` in the containing scope. How can I do this?

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [August 7, 2022, 8:36am UTC](https://discourse.julialang.org/t/let-in-macro/85431/2 "2022-08-07T08:36:00Z")

</div>

Do you know about `let` blocks?

[https://docs.julialang.org/en/v1/manual/variables-and-scoping/#Let-Blocks](https://docs.julialang.org/en/v1/manual/variables-and-scoping/#Let-Blocks)

---

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [August 7, 2022, 8:46am UTC](https://discourse.julialang.org/t/let-in-macro/85431/3 "2022-08-07T08:46:33Z")

</div>

A `let` block can do this but the names `x` and `y` are far away from their values which makes it harder to read and easier to get out of synch.

```julia
x, y = let
    a = 1
    b = 2
    a + b, a - b
end

```

---

<div class="post-metadata">

**Author:** ![digital\_carver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/digital_carver/32/33818_2.png) [@digital\_carver](https://discourse.julialang.org/u/digital_carver)\
**Post date:** [August 8, 2022, 12:01pm UTC](https://discourse.julialang.org/t/let-in-macro/85431/4 "2022-08-08T12:01:02Z")

</div>

You can return a `NamedTuple` from the `let` block and destructure it for the assignment:

```julia
julia> (; x, y) = let a = 1, b = 2
           (x = a + b, y = a - b)
       end
(x = 3, y = -1)

julia> x
3

```

---

<div class="post-metadata">

**Author:** ![johnmyleswhite](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnmyleswhite/32/31_2.png) [@johnmyleswhite](https://discourse.julialang.org/u/johnmyleswhite)\
**Post date:** [August 8, 2022, 12:30pm UTC](https://discourse.julialang.org/t/let-in-macro/85431/5 "2022-08-08T12:30:51Z")

</div>

This thread seems to have wandered off from your original question. What specifically about authoring this macro do you need help with?

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [August 8, 2022, 12:34pm UTC](https://discourse.julialang.org/t/let-in-macro/85431/6 "2022-08-08T12:34:35Z")

</div>

You can do this instead, if you want y and z to be available outside, but not a and b:

```julia
begin
    local a = 1
    local b = 2
    y = a + b
    z = a - b
end

```

---

<div class="post-metadata">

**Author:** ![arik](https://avatars.discourse-cdn.com/v4/letter/a/c4cdca/32.png) [@arik](https://discourse.julialang.org/u/arik)\
**Post date:** [August 8, 2022, 1:03pm UTC](https://discourse.julialang.org/t/let-in-macro/85431/7 "2022-08-08T13:03:29Z")

</div>

> [@jar1](#):
>
> ```julia
> @let begin
> a = 1
> b = 2
> in
> y = a + b
> z = a - b
> end
> 
> ```

What should this macro be translated into?

When designing macros, there are in general two questions one needs to answer:

- What expressions are valid for that macro?
- How should the result look like?

Four your example I think a valid expression for your `@let` macro might look like this:

- Its a `:block` expression, and
- one of its lines consists just of the symbol `:in`, and
- all of the lines before that are simple assignments of the form `<varname> = ...`

But what should this do, what should it turn into? Do you want the result to look like this?

```julia
z, y = let a=1, b=2
    y = a + b
    z = a - b
    z, y
end
```
