ifInstanceOf

The intention of this closure method is to take the input object 'obj', check if it matches the required type, and then call the closure itself with the typecast object 'str'. Thus, this operation is the closure equivalent of this current operation:

Unfortunately, this won't compile, as you can't write obj instanceof T where T is a generic type. The problem is of course erasure. This method has no way of determining what the actual type of T is - String in this example. So what can be done?

One solution would be to pass the type in as an additional parameter from the calling code:

This would be fairly simple to do, and in fact could probably be optimised by the compiler so that the actual types are only stored if the actualType method is called.

I'm not sure if this is the only use-case where the actual types of the closure would be useful, but I suspect it isn't. If you've got another use-case where the actual type would be useful, or any other feedback, please add a comment!

I think any hacks to magically add limited reification should be held off and if reification is deemed to be needed (which this example doesn't prove), then the process would be clean, and wouldn't involve reverting such hacks.

Plus, haven't we left casting back in 1.4? I'm doing my best to believe that!