From: Bard B. <ba...@us...> - 2012-06-08 21:08:00
|
Here's a little X10 puzzle. I've got a *Java* method I need to call from X10: table.get(get); This method can throw java.io.IOException, so I have to wrap calls to it in a try/catch block. If I don't, I get Java compilation errors: x10c: ---------- 1. ERROR in clsss/xjj/HTabler.java (at line 474) thingTable.get(((org.apache.hadoop.hbase.client.Get)(this. get))); ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Unhandled exception type IOException ---------- 1 problem (1 error) x10c: Non-zero return code: 255 So I have to put a try/catch around it: try { return thingTable.get(get); } catch(e:x10.lang.Exception) { e.printStackTrace(); throw new RuntimeException("Oh no from HTabler.retrieveThingRecord! " + e); } And that compiles fine. (I'm a bit confused about why, since I didn't think java.io.IOException was consistent with x10.lang.Exception.) But what I really want to do is write a method that behaves like various things named 'with' in LISP and Python and such. public static def withThingTable[T]( body : (HTableInterface) => T ) {T haszero} : T So that withThingTable(useThingTable) will call the function useThingTable on a properly-opened Thing Table, and, after it's done (even exceptionally) will properly close it. withThingTable is pretty careful with catching exceptions, so it should run OK. It's a convenience method, encapsulating a required programming pattern. The point of a convenience method is that it be convenient. I *want* to write: return withThingTable( (tt:HTableInterface) => tt.get(get) ) But X10 and Java don't know that I'm being careful with exceptions. So I have to write the try/catch block inside the closure: return withThingTable[Result]((thingTable:HTableInterface) => { try { return thingTable.get(get); } catch(e:x10.lang.Exception) { e.printStackTrace(); throw new RuntimeException("Oh no from HTabler.retrieveThingRecord! " + e); } }); And this makes my convenience method not so convenient. Any suggestions about how I could avoid having to write the try/catch block in the closure? Preferably without having to write my own 'get' (and all N other HBase methods)? Thanks, Bard |