# D lang scope guards in Julia

**URL:** <https://discourse.julialang.org/t/d-lang-scope-guards-in-julia/3229>\
**Category:** Internals & Design\
**Tags:** proposal\
**Created:** [April 15, 2017, 4:26am UTC](https://discourse.julialang.org/t/d-lang-scope-guards-in-julia/3229 "2017-04-15T04:26:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![musm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/musm/32/3675_2.png) [@musm](https://discourse.julialang.org/u/musm)\
**Post date:** [April 15, 2017, 4:26am UTC](https://discourse.julialang.org/t/d-lang-scope-guards-in-julia/3229/1 "2017-04-15T04:26:21Z")

</div>

I think julia should incorporate (“steal”) D language type scope guards  
ref [Scope guards - Dlang Tour](https://tour.dlang.org/tour/en/gems/scope-guards)  
The examples there and in the references make it very clear why such a feature is desirable and useful. I think Julia would benefit from this being baked in at the language level.

---

<div class="post-metadata">

**Author:** ![fengyang.wang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fengyang.wang/32/104_2.png) [@fengyang.wang](https://discourse.julialang.org/u/fengyang.wang)\
**Post date:** [April 15, 2017, 4:33am UTC](https://discourse.julialang.org/t/d-lang-scope-guards-in-julia/3229/2 "2017-04-15T04:33:56Z")

</div>

This is somewhat similar in spirit to [https://github.com/adambrewster/Defer.jl](https://github.com/adambrewster/Defer.jl). Note that I have not used that package and can’t endorse that it will suit your use case.

cc @adambrewster

---

<div class="post-metadata">

**Author:** ![musm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/musm/32/3675_2.png) [@musm](https://discourse.julialang.org/u/musm)\
**Post date:** [April 15, 2017, 2:56pm UTC](https://discourse.julialang.org/t/d-lang-scope-guards-in-julia/3229/3 "2017-04-15T14:56:04Z")

</div>

Here are some examples of scope guards to illustrate it’s effect

```julia
<statement1>
scope(exit) <statement2>
<statement3>

```

which lowers to

```julia
<statement1>
try
    <statement3>
finally
    <statement2>
end

```

More examples

```julia
<statement1>
scope(exit) <statement2>
<statement3>
scope(exit) <statement4>
<statement5>

```

which lowers to

```julia
<statement1>
try
    <statement3>
        try
            <statement5>
        finally
            <statement4>
        end
finally
    <statement2>
end

```

Another example

```julia
<statement1>
scope(success) <statement2>
<statement3>

```

lowers to

```julia
<statement1>
success = true
try
    <statement3>
catch e
    success = false
    throw(e)
finally
    if success
        <statement2>
    end
end

```

---

<div class="post-metadata">

**Author:** ![adambrewster](https://avatars.discourse-cdn.com/v4/letter/a/ee59a6/32.png) [@adambrewster](https://discourse.julialang.org/u/adambrewster)\
**Post date:** [April 19, 2017, 3:51pm UTC](https://discourse.julialang.org/t/d-lang-scope-guards-in-julia/3229/4 "2017-04-19T15:51:14Z")

</div>

Defer.jl handles the `scope(exit)` case. Note that I use the word `@scope` differently and the `@defer` macro is more like your `scope(exit)`.

Your example

```julia
<statement1>
scope(exit) <statement2>
<statement3>
scope(exit) <statement4>
<statement5>

```

could be written as

```julia
using Defer
@scope begin
  <statement1>
  @defer <statement2>
  <statement3>
  @defer <statement4>
  <statement5>
end

```
