# How to (kind of) emulate RAII using \`defer\`

**URL:** <https://forums.swift.org/t/how-to-kind-of-emulate-raii-using-defer/63719>\
**Category:** Using Swift\
**Tags:** defer-statement\
**Created:** [March 15, 2023, 3:36am UTC](https://forums.swift.org/t/how-to-kind-of-emulate-raii-using-defer/63719 "2023-03-15T03:36:00Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![ksluder](https://avatars.discourse-cdn.com/v4/letter/k/eada6e/32.png) [@ksluder](https://forums.swift.org/u/ksluder)\
**Post date:** [March 15, 2023, 1:30pm UTC](https://forums.swift.org/t/how-to-kind-of-emulate-raii-using-defer/63719/4 "2023-03-15T13:30:40Z")

</div>

> [@eskimo](#):
>
> First, I’m not a big fan of the C++ RAII model because it ties resource management to memory management.

I would go so far as to say the RAII pattern exists to emulate `defer` in languages that lack it. There is no need for RAII in Swift because Swift has `defer`.

---

_[View the full topic](https://forums.swift.org/t/how-to-kind-of-emulate-raii-using-defer/63719)._
