# 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:** 1
**Showing post:** 8

<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.

---

_[View the full topic](https://discourse.julialang.org/t/is-there-a-julia-function-similar-to-the-r-function-with/91493)._
