# Is there a Julia function similar to the R function \`with\`?

**URL:** <https://discourse.julialang.org/t/is-there-a-julia-function-similar-to-the-r-function-with/91493>\
**Category:** General Usage\
**Tags:** function\
**Created:** [December 9, 2022, 8:56pm UTC](https://discourse.julialang.org/t/is-there-a-julia-function-similar-to-the-r-function-with/91493 "2022-12-09T20:56:34Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![\_stla](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/_stla/32/35613_2.png) [@\_stla](https://discourse.julialang.org/u/_stla)\
**Post date:** [December 9, 2022, 8:56pm UTC](https://discourse.julialang.org/t/is-there-a-julia-function-similar-to-the-r-function-with/91493/1 "2022-12-09T20:56:34Z")

</div>

Hello,

In R, when you have a list like `mylist <- list(a = 2, b = 3)`, then instead of doing `mylist$a + mylist$b` you can do `with(mylist, a + b)`. Is there something similar in Julia for a named tuple like `(a = 2, b = 3)`?

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [December 9, 2022, 10:41pm UTC](https://discourse.julialang.org/t/is-there-a-julia-function-similar-to-the-r-function-with/91493/2 "2022-12-09T22:41:24Z")

</div>

Can you clarify what exactly you are trying to do? We have similar syntax with tables, but maybe you don’t even need the syntax and there is a better way to achieve your goal with idiomatic Julia.

---

<div class="post-metadata">

**Author:** ![albheim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albheim/32/34660_2.png) [@albheim](https://discourse.julialang.org/u/albheim)\
**Post date:** [December 9, 2022, 10:53pm UTC](https://discourse.julialang.org/t/is-there-a-julia-function-similar-to-the-r-function-with/91493/3 "2022-12-09T22:53:04Z")

</div>

Not exactly what you ask for, but you have the let-statement where you can do a small destructure

```julia
julia> mytup = (a=3, b=4)
(a = 3, b = 4)

julia> let (; a, b) = mytup
       a + b
       end
7

```

---

<div class="post-metadata">

**Author:** ![\_stla](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/_stla/32/35613_2.png) [@\_stla](https://discourse.julialang.org/u/_stla)\
**Post date:** [December 9, 2022, 11:08pm UTC](https://discourse.julialang.org/t/is-there-a-julia-function-similar-to-the-r-function-with/91493/4 "2022-12-09T23:08:55Z")

</div>

@juliohm I have a large tuple `params = (a = 1, b = 2, ..., j = 10)` and a function with arguments `a`, `b`, …, `j`, I don’t want to type `f(params.a, params.b, ..., params.j)`.

Hmm… but I just found a solution for this situation: `f(params...)` works.

---

<div class="post-metadata">

**Author:** ![\_stla](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/_stla/32/35613_2.png) [@\_stla](https://discourse.julialang.org/u/_stla)\
**Post date:** [December 9, 2022, 11:11pm UTC](https://discourse.julialang.org/t/is-there-a-julia-function-similar-to-the-r-function-with/91493/5 "2022-12-09T23:11:52Z")

</div>

But well, what if `params` is not ordered as the arguments of the function?

---

<div class="post-metadata">

**Author:** ![brendaisy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brendaisy/32/32810_2.png) [@brendaisy](https://discourse.julialang.org/u/brendaisy)\
**Post date:** [December 9, 2022, 11:15pm UTC](https://discourse.julialang.org/t/is-there-a-julia-function-similar-to-the-r-function-with/91493/6 "2022-12-09T23:15:36Z")

</div>

This has been discussed before, for example [here](https://discourse.julialang.org/t/is-there-a-with-operator-or-usage-pattern/52693/11) and [here](https://discourse.julialang.org/t/unpack-named-tuples-does-anyone-know-how-to-do-it/32456).

My impulse is to accomplish something like this with function keywords, e.g.

```julia
julia> t = (a=2, b=3)
(a = 2, b = 3)

julia> foo(;a, b) = a + b
foo (generic function with 1 method)

julia> foo(;t...)
5

```

---

<div class="post-metadata">

**Author:** ![bertschi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bertschi/32/33462_2.png) [@bertschi](https://discourse.julialang.org/u/bertschi)\
**Post date:** [December 9, 2022, 11:18pm UTC](https://discourse.julialang.org/t/is-there-a-julia-function-similar-to-the-r-function-with/91493/7 "2022-12-09T23:18:12Z")

</div>

If you don’t need different methods for `f` you could define it with keyword arguments and call it by splatting those, i.e., `f(; params...)` as also suggested by @brendaisy.  
In general, to replicate the functionality of R requires non-standard evaluation and could be done with a macro in Julia. The package `StatsPlots` has a similar macro for tables which allows you to write: `@df mytable :a + :b`. You could check their implementation and build something similar for named tuples. Yet, given the short syntax for unpacking names tuples as shown by @albheim a macro might not be necessary here.

---

<div class="post-metadata">

**Author:** ![uniment](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uniment/32/24532_2.png) [@uniment](https://discourse.julialang.org/u/uniment)\
**Post date:** [December 10, 2022, 3:40am UTC](https://discourse.julialang.org/t/is-there-a-julia-function-similar-to-the-r-function-with/91493/8 "2022-12-10T03:40:36Z")

</div>

For fun.

```julia
julia> macro with(container, expressions...)
           esc(:( let (; $(propertynames(getfield( __module__ , container))...))=$container
               $(expressions...)
           end ))
       end
@with (macro with 1 method)

julia> let t=(a=2, b=3)
           @with t a+b
       end
5

julia> tup = NamedTuple(Symbol(c)=>c for c ∈ 'a':'j')
(a = 'a', b = 'b', c = 'c', d = 'd', e = 'e', f = 'f', g = 'g', h = 'h', i = 'i', j = 'j')

julia> @with tup (j, i, h, g, f, e, d, c, b, a)
('j', 'i', 'h', 'g', 'f', 'e', 'd', 'c', 'b', 'a')

julia> tup = (a=1, b=2, c=3)
(a = 1, b = 2, c = 3)

julia> @with tup begin
           local temp=a+b
           temp+c
       end
6

```

You can compare this against [this macro](https://discourse.julialang.org/t/is-there-a-with-operator-or-usage-pattern/52693/13). But you should also consider whether such a thing is even worthwhile, considering [this](https://discourse.julialang.org/t/is-there-a-with-operator-or-usage-pattern/52693/14).

## _Edit:_

Oops, I polluted my global namespace. The above code does not, infact, work for locally-declared named tuples; it only works for globally-visible objects.

Looks like [this](https://discourse.julialang.org/t/is-there-a-with-operator-or-usage-pattern/52693/18) works though. It’s quite clever.
