# Is there standard way to use multiple resources?

**URL:** <https://discuss.kotlinlang.org/t/is-there-standard-way-to-use-multiple-resources/2613>\
**Category:** Libraries\
**Created:** [May 16, 2017, 6:20am UTC](https://discuss.kotlinlang.org/t/is-there-standard-way-to-use-multiple-resources/2613 "2017-05-16T06:20:59Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![vbezhenar](https://avatars.discourse-cdn.com/v4/letter/v/35a633/32.png) [@vbezhenar](https://discuss.kotlinlang.org/u/vbezhenar)\
**Post date:** [May 16, 2017, 6:20am UTC](https://discuss.kotlinlang.org/t/is-there-standard-way-to-use-multiple-resources/2613/1 "2017-05-16T06:20:59Z")

</div>

Something like

```
getConnection().use { connection ->
  connection.createStatement().use { statement ->
    statement.executeQuery(sql).use { resultSet ->
      while (resultSet.next()) { ... }
    }
  }
}

```

looks weird with multiple nested levels for multiple resources. I rolled my own simple implementation for multiple resources:

```
class Resources : AutoCloseable {
  val resources = mutableListOf<AutoCloseable>()

  fun <T: AutoCloseable> T.use(): T {
    resources += this
    return this
  }

  override fun close() {
    var exception: Exception? = null
    for (resource in resources.reversed()) {
      try {
        resource.close()
      } catch (closeException: Exception) {
        if (exception == null) {
          exception = closeException
        } else {
          exception.addSuppressed(closeException)
        }
      }
    }
    if (exception != null) throw exception
  }
}

inline fun <T> withResources(block: Resources.() -> T): T = Resources().use(block)

```

which could be used:

```
withResources {
  val connection = getConnection().use()
  val statement = connection.createStatement().use()
  val resultSet = statement.executeQuery(sql).use()
  while (resultSet.next()) { ... }
}

```

This works for me, but I wonder if there’s better way with standard library or something like that.

---

<div class="post-metadata">

**Author:** ![yole](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/yole/32/1425_2.png) [@yole](https://discuss.kotlinlang.org/u/yole)\
**Post date:** [May 16, 2017, 9:57am UTC](https://discuss.kotlinlang.org/t/is-there-standard-way-to-use-multiple-resources/2613/2 "2017-05-16T09:57:05Z")

</div>

No, there’s no standard solution at this time.

---

<div class="post-metadata">

**Author:** ![fengl](https://avatars.discourse-cdn.com/v4/letter/f/f19dbf/32.png) [@fengl](https://discuss.kotlinlang.org/u/fengl)\
**Post date:** [September 20, 2017, 10:40am UTC](https://discuss.kotlinlang.org/t/is-there-standard-way-to-use-multiple-resources/2613/3 "2017-09-20T10:40:11Z")

</div>

Hello there,  
i had the same problem in my project, so i generated multiple inline functions based on the “using”-keyword from C#.

Maybe this is also usefull to you:

> **[GitHub - FelixEngl/KotlinUsings: An implementation of the using-Keyword from...](https://github.com/FelixEngl/KotlinUsings)**
>
> An implementation of the using-Keyword from C# as inline function for Kotlin. - GitHub - FelixEngl/KotlinUsings: An implementation of the using-Keyword from C# as inline function for Kotlin.

Best Regards,

Felix

---

<div class="post-metadata">

**Author:** ![jensbarthel](https://avatars.discourse-cdn.com/v4/letter/j/96bed5/32.png) [@jensbarthel](https://discuss.kotlinlang.org/u/jensbarthel)\
**Post date:** [February 11, 2019, 4:42pm UTC](https://discuss.kotlinlang.org/t/is-there-standard-way-to-use-multiple-resources/2613/4 "2019-02-11T16:42:01Z")

</div>

Hi,  
I’m looking for the same thing. Has there been any development?

---

<div class="post-metadata">

**Author:** ![jhult](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/jhult/32/6108_2.png) [@jhult](https://discuss.kotlinlang.org/u/jhult)\
**Post date:** [April 19, 2019, 10:21pm UTC](https://discuss.kotlinlang.org/t/is-there-standard-way-to-use-multiple-resources/2613/5 "2019-04-19T22:21:59Z")

</div>

@ [vbezhenar](https://discuss.kotlinlang.org/u/vbezhenar), thank you for your implementation. It works great and is elegant.

---

<div class="post-metadata">

**Author:** ![8kac8](https://avatars.discourse-cdn.com/v4/letter/8/f07891/32.png) [@8kac8](https://discuss.kotlinlang.org/u/8kac8)\
**Post date:** [February 19, 2021, 9:32am UTC](https://discuss.kotlinlang.org/t/is-there-standard-way-to-use-multiple-resources/2613/6 "2021-02-19T09:32:16Z")

</div>

Lovely solution, thank you.  
Could you let us in on a little secret, how you injected  
`block: Resources.() -> T`  
into a `use` with signature  
`block: (Resources) -> T`  
?

* * *

Edit: it/this receivers can be interchanged, didn’t know that.
