# Is Go's defer possible in Kotlin?

**URL:** <https://discuss.kotlinlang.org/t/is-gos-defer-possible-in-kotlin/625>\
**Category:** Uncategorized\
**Created:** [March 18, 2014, 1:38pm UTC](https://discuss.kotlinlang.org/t/is-gos-defer-possible-in-kotlin/625 "2014-03-18T13:38:35Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jrenner](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/jrenner/32/1472_2.png) [@jrenner](https://discuss.kotlinlang.org/u/jrenner)\
**Post date:** [March 18, 2014, 1:38pm UTC](https://discuss.kotlinlang.org/t/is-gos-defer-possible-in-kotlin/625/1 "2014-03-18T13:38:35Z")

</div>

Is something like Go's defer control structure possible in Kotlin? (link: [http://golangtutorials.blogspot.tw/2011/06/control-structures-go-defer-statement.html](http://golangtutorials.blogspot.tw/2011/06/control-structures-go-defer-statement.html))

If not, could I make it a feature request? 🙂

Or, should I just stick with try/catch/finally?

---

<div class="post-metadata">

**Author:** ![abreslav](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/abreslav/32/8279_2.png) [@abreslav](https://discuss.kotlinlang.org/u/abreslav)\
**Post date:** [March 18, 2014, 3:25pm UTC](https://discuss.kotlinlang.org/t/is-gos-defer-possible-in-kotlin/625/2 "2014-03-18T15:25:44Z")

</div>

You can do something similar as a library function, like:

```kotlin
withDefers {
   foo()
   defer { bar() }
   baz()
}
```

See code here: [http://kotlin-demo.jetbrains.com/?publicLink=1040749715610173087711460271843](http://kotlin-demo.jetbrains.com/?publicLink=1040749715610173087711460271843)

---

<div class="post-metadata">

**Author:** ![MarioArias](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/marioarias/32/1488_2.png) [@MarioArias](https://discuss.kotlinlang.org/u/MarioArias)\
**Post date:** [March 18, 2014, 4:22pm UTC](https://discuss.kotlinlang.org/t/is-gos-defer-possible-in-kotlin/625/3 "2014-03-18T16:22:38Z")

</div>

Because Golang's defer is a LIFO structure you must change your done function to

```kotlin
fun done() {
     for (action in actions.reverse()) {
          action()
     }
}
```

Or using a Stack structure instead of list

---

<div class="post-metadata">

**Author:** ![abreslav](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/abreslav/32/8279_2.png) [@abreslav](https://discuss.kotlinlang.org/u/abreslav)\
**Post date:** [March 18, 2014, 4:24pm UTC](https://discuss.kotlinlang.org/t/is-gos-defer-possible-in-kotlin/625/4 "2014-03-18T16:24:39Z")

</div>

Good point. Fixed

---

<div class="post-metadata">

**Author:** ![jrenner](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/jrenner/32/1472_2.png) [@jrenner](https://discuss.kotlinlang.org/u/jrenner)\
**Post date:** [March 18, 2014, 4:58pm UTC](https://discuss.kotlinlang.org/t/is-gos-defer-possible-in-kotlin/625/5 "2014-03-18T16:58:32Z")

</div>

Very nice.&nbsp;&nbsp;One question, what is going on in L13-17? Is that some short cut to declaring an extension method? I have also not seen the "WithDefers.() -\> ..…" syntax on L36 before, is saying type is a method of WithDefers?

---

<div class="post-metadata">

**Author:** ![abreslav](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/abreslav/32/8279_2.png) [@abreslav](https://discuss.kotlinlang.org/u/abreslav)\
**Post date:** [March 18, 2014, 5:17pm UTC](https://discuss.kotlinlang.org/t/is-gos-defer-possible-in-kotlin/625/6 "2014-03-18T17:17:24Z")

</div>

This is called "type-safe builders". Essentially these are function calls that take lambdas as arguments.

For more information check out this page: [http://confluence.jetbrains.com/display/Kotlin/Type-safe](http://confluence.jetbrains.com/display/Kotlin/Type-safe)+Groovy-style+builders  
And this article has an extensive tutorial: [http://jaxenter.com/type-safe-web-with-kotlin-47395.html](http://jaxenter.com/type-safe-web-with-kotlin-47395.html)
