# C# using() in Kotlin

**URL:** <https://discuss.kotlinlang.org/t/c-using-in-kotlin/1051>\
**Category:** Uncategorized\
**Created:** [October 24, 2012, 6:36pm UTC](https://discuss.kotlinlang.org/t/c-using-in-kotlin/1051 "2012-10-24T18:36:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Flow](https://avatars.discourse-cdn.com/v4/letter/f/8edcca/32.png) [@Flow](https://discuss.kotlinlang.org/u/Flow)\
**Post date:** [October 24, 2012, 6:36pm UTC](https://discuss.kotlinlang.org/t/c-using-in-kotlin/1051/1 "2012-10-24T18:36:37Z")

</div>

Hi!

I experimented with implementing something along the lines of C# using construct in Kotlin. Maybe a built-in support still is to be preferred, but it looks quite nice anyway.

``

```kotlin
import java.io.File
import java.io.FileReader
fun unwindProtect<T, R>(create: () -> T, transform: (T) -> R, cleanup: (T) -> Unit): R {
  val value = create()
  try {
  return transform(value)
  } finally {
  cleanup(value)
  }
}
fun withOpenFile<T>(filename: String, body: (FileReader) -> T): T {
  return unwindProtect({ File(filename).reader() }, body, { f -> f.close() })
}
fun main(args: Array<String>) {
  withOpenFile(“hello.txt”, { reader -> println(reader.readText()) })
}
```

unwindProtect() is a helper function. The name comes from Common Lisps unwind-protect.  
The only thing that really could be improved here is that I had to use “reader -\>” to introduce a name for the passed value. I suppose one could use “it” instead and skip that.

So what do you people think about this? Is special syntax for this common pattern needed?  
I think I would like to have a special syntax, but I’m used to C#.

Regards, Flow

---

<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:** [October 24, 2012, 8:42pm UTC](https://discuss.kotlinlang.org/t/c-using-in-kotlin/1051/2 "2012-10-24T20:42:35Z")

</div>

We are on the fence here. See this discussion: [http://youtrack.jetbrains.com/issue/KT-2984](http://youtrack.jetbrains.com/issue/KT-2984)

---

<div class="post-metadata">

**Author:** ![jstrachan](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/jstrachan/32/1559_2.png) [@jstrachan](https://discuss.kotlinlang.org/u/jstrachan)\
**Post date:** [October 25, 2012, 8:39am UTC](https://discuss.kotlinlang.org/t/c-using-in-kotlin/1051/3 "2012-10-25T08:39:10Z")

</div>

As an aside the standard library has a 'use' extension function on any java.lang.Closeable type such as the java.io streams and reader/writers so you can do things like...

> FileReader("foo.txt").use { reader -\> &nbsp;&nbsp;... }

which ensures propery try/catch and closing behaviour. The issue Andrey brings up is around being able to use multiple resources in a more DRY syntax (rather like java's try-with-resources syntax or Scala's for statement).
