Thanks to Stuart's prodding, the next version of ikvmc will automatically add a
System.Collections.IEnumerable implementation to any Java classes that implement
That means that you can now do this in C#:
java.util.ArrayList list = new java.util.ArrayList();
using (java.io.FileWriter fw = new java.io.FileWriter("test.txt"))
foreach (string s in list)
Note that the interfaces are only added to the classes (i.e. the
java.io.Closeable interface doesn't implement
System.IDisposable). From Java this change is mostly invisible, e.g. when you use Java reflection you won't see
java.io.FileWriter. However, when you use the
instanceof operator or do a cast to
cli.System.IDisposable, it will succeed. It would be possible to remove this inconsistency, but I don't think that's worth it.
Too Much Magic
Too much of anything is never a good thing, but in the case of adding magic to ikvm, it has always been very difficult to draw the line between just enough and too much. There already is a fair bit a magic in ikvm and I tend to be rather conservative in adding more. The main reason is that magic is almost never "perfect", this means that in most cases it will work as expected, but there are always edge cases where people will be surprised by how things work and that's clearly not good.
Stuart also wants Java's foreach construct to work on .NET types that implement IEnumerable, but to make that work would require turning
java.lang.Iterable into a ghost interface and I currently feel that would be in the "too much magic" category, especially considering that ghost interfaces are not very good magic.