Bind java.lang.AutoCloseable
as IDisposable
#484
Labels
enhancement
Proposed change to current functionality
generator
Issues binding a Java library (generator, class-parse, etc.)
Context: dotnet/android@d3e5974
Background
Java 8 introduced a
try-with-resources
statement, which is conceptually similar to the C#using
block:The Java compiler expands this into a
try-finally
block:try-with-resources
can be used with any type which implementsjava.lang.AutoCloseable
.Additionally,
java.io.Closeable
now implementsjava.lang.AutoCloseable
, so alljava.io
constructs supporttry-with-resources
syntax.Binding
generator
should natively support this feature: if a type implementsJava.Lang.IAutoCloseable
, then generation for the type should overrideJava.Lang.Object.Dispose(bool)
to have it callClose()
.For example,
Java.IO.FileOutputStream
implementsJava.IO.ICloseable
, and thus it implementsJava.Lang.IAutoCloseable
.generator
should thus emit:By doing so, C#
using
blocks work as expected:Open Questions
Do we need to worry about calling
Close()
multiple times?FileOutputStream
inherits fromOutputStream
, both of which implementICloseable
. Will it be a problem when bothOutputStream
andFileOutputStream
overrideObject.Dispose(bool)
to callClose()
?I believe this won't be a problem, as the
Closeable.close()
docs state:The text was updated successfully, but these errors were encountered: