title: try-with-resources: "use" extension function in Kotlin does not always work
body: <p>I had some trouble expressing the Java's <a href="https://docs.oracle.com/javase/7/docs/technotes/guides/language/try-with-resources.html" rel="noreferrer">try-with-resources</a> construct in Kotlin. In my understanding, every expression that is an instance of <code>AutoClosable</code> should provide the <code>use</code> extension function.</p>
  
  <p>Here is a complete example:</p>
  
  <pre><code>import java.io.BufferedReader;
  import java.io.FileReader;
  
  import org.openrdf.query.TupleQuery;
  import org.openrdf.query.TupleQueryResult;
  
  public class Test {
  
      static String foo(String path) throws Throwable {
          try (BufferedReader r =
             new BufferedReader(new FileReader(path))) {
            return "";
          }
      }
  
      static String bar(TupleQuery query) throws Throwable {
          try (TupleQueryResult r = query.evaluate()) {
            return "";
          }
      }
  }
  </code></pre>
  
  <p>The Java-to-Kotlin converter creates this output:</p>
  
  <pre><code>import java.io.BufferedReader
  import java.io.FileReader
  
  import org.openrdf.query.TupleQuery
  import org.openrdf.query.TupleQueryResult
  
  object Test {
  
      @Throws(Throwable::class)
      internal fun foo(path: String): String {
          BufferedReader(FileReader(path)).use { r -&gt; return "" }
      }
  
      @Throws(Throwable::class)
      internal fun bar(query: TupleQuery): String {
          query.evaluate().use { r -&gt; return "" } // ERROR
      }
  }
  </code></pre>
  
  <p><code>foo</code> works fine, but the code in <code>bar</code> does not compile: </p>
  
  <pre><code>Error:(16, 26) Kotlin: Unresolved reference.
  None of the following candidates is applicable
  because of receiver type mismatch: 
  public inline fun &lt;T : java.io.Closeable, R&gt;
  ???.use(block: (???) -&gt; ???): ??? defined in kotlin.io
  </code></pre>
  
  <p><code>query.evaluate()</code> is from <a href="http://rdf4j.org/" rel="noreferrer">Sesame</a> and implements <code>AutoClosable</code>. Is it a Kotlin bug, or is there a reason why it does not work?</p>
  
  <hr>
  
  <p>I am using IDEA 15.0.3 with Kotlin 1.0.0-beta-4584-IJ143-12 and the following <code>sasame-runtime</code> version:</p>
  
  <pre><code>&lt;groupId&gt;org.openrdf.sesame&lt;/groupId&gt;
  &lt;artifactId&gt;sesame-runtime&lt;/artifactId&gt;
  &lt;version&gt;4.0.2&lt;/version&gt;
  </code></pre>