tag:blogger.com,1999:blog-741750605858169835.post5071815410281541889..comments2024-01-24T14:53:02.919+00:00Comments on Stephen Colebourne's blog: Adding closures to Java, or not...Stephen Colebournehttp://www.blogger.com/profile/01454237967846880639noreply@blogger.comBlogger3125tag:blogger.com,1999:blog-741750605858169835.post-53149442777859338392005-11-25T19:31:41.000+00:002005-11-25T19:31:41.000+00:00Looks like what you are asking for is actually fir...Looks like what you are asking for is actually first class functions in Java. <br /><br />The following scenario occurs often.<br /><br />You know that after using your resource, you want to close it whether or not it succeeded. So, if you have a method m in class c that does the work, you don't want to invoke the code in the finally block.<br /><br />Assuming functions could be easily passed in, the code would look somewhat like this:<br /><br />public void useResource(Function f, Resource r) {<br /> try {<br /> f(r);<br /> }<br /> finally {<br /> r.close();<br /> }<br />}<br /><br />This method could then be used as often as necessary secure in the knowledge that the Resource would always be closed. Standard error handling may also be easily included with a catch block or two.<br /><br />But this is definitely not what closures are about.VRnoreply@blogger.comtag:blogger.com,1999:blog-741750605858169835.post-61509173477774321862005-11-21T10:27:08.000+00:002005-11-21T10:27:08.000+00:00Kirk, I understand that this isn't closures. H...Kirk, I understand that this isn't closures. However, I do believe that you can't have closures in Java without something like this.<br /><br />In groovy, they added a forEach() method to Collection. In Java we can't do that. So, we need some way to 'manage' the closure. That is what my proposal actually covers.<br /><br />My final comment is that once you do this, you may find that 80% of the times you think you need closures, you actually don't - a surrounded code block is sufficient.Stephen Colebournenoreply@blogger.comtag:blogger.com,1999:blog-741750605858169835.post-88250861684986280932005-11-21T07:20:46.000+00:002005-11-21T07:20:46.000+00:00For good information on what a closure is check ou...For good information on what a closure is check out http://c2.com/cgi/wiki?WhatIsClosure.Kirkhttp://www.javaperformancetuning.comnoreply@blogger.com