tag:blogger.com,1999:blog-741750605858169835.post2264590925638535536..comments2024-01-24T14:53:02.919+00:00Comments on Stephen Colebourne's blog: Comparing closures (2 more examples) - CICE, BGGA and FCMStephen Colebournehttp://www.blogger.com/profile/01454237967846880639noreply@blogger.comBlogger6125tag:blogger.com,1999:blog-741750605858169835.post-85158457013332808312007-03-08T18:19:45.000+00:002007-03-08T18:19:45.000+00:00Howard, I find some of the ideas presented in C3S ...Howard, I find some of the ideas presented in C3S quite interesting. Actually inspiring. But I second Stephen, it looks alien wrt. Java, is in some details not clear to me, and might also cause compilation problems (e.g. using a method without parens might conflict with a field).Stefan Schulzhttp://jroller.com/page/jaddanoreply@blogger.comtag:blogger.com,1999:blog-741750605858169835.post-5429587092569395762007-03-08T01:58:02.000+00:002007-03-08T01:58:02.000+00:00@Stephen,
I was interested in your comments:
1....@Stephen,<br /><br />I was interested in your comments:<br /><br /><br />1. A different language<br />=======================<br /><br />I would say the same language, Java, with different syntax:<br /><br />1a. Short syntax for inner classes and most particularly those that have just one abstract method. The use of the keyword method is similar to Javascript, but creates an instance of an inner class.<br /><br />1b. Syntax that is based around the construct keyword ( ... ) { ... }. Like if, for, etc. <br /><br />1c. Type inference within a statement, but not between statements. Like Scala, but not as extensive as Haskel or ML. <br /><br />1d. } finishes a statement so ; isn't necessary before a }. Like end in Pascal.<br /><br />1e. () are not required for methods that have no arguments, provided that it doesn't clash with a field. Like Ruby, Groovy, etc.<br /><br />1f. () are not required for the last in a chain of top level method calls. Like Ruby.<br /><br /><br />2. Method instead of #<br />======================<br /><br />The two constructs are similar, I favour a keyword and in particular sticking with the standard C/C++/Java constuct of keyword ( ... ) { ... }. The method # constructs are similar except that method creates an inner class. The method construct is similar to the # construct in that inner classes are extended in C3S so that final isn't necessary on local variables.<br /><br />You have mentioned in a previous blog that you see the main difference between BGGA closure and inner classes as the treatment of this. I would agree with that; but add that an inner class has two this pointers, one to the enclosing instance and one to the inheriting instance and a BGGA closure has just the this pointer to the enclosing instance. Therefore everything you can do with a BGGA closure you can do with an inner class. But tellingly the reverse isn't true, i.e. the inner class is the more powerful construct.Howard Lovattnoreply@blogger.comtag:blogger.com,1999:blog-741750605858169835.post-21712522104864729122007-03-07T15:34:25.000+00:002007-03-07T15:34:25.000+00:00@Howard, Thanks for the examples. I hadn't rea...@Howard, Thanks for the examples. I hadn't really looked at C3S until now, and wasn't really thinking of it as a closure proposal. For me, C3S is almost a different programming language - it's certainly not clear. The use of a method keyword is an interesting concept however - we just used # instead.Stephen Colebournenoreply@blogger.comtag:blogger.com,1999:blog-741750605858169835.post-938547840960117782007-03-07T04:54:09.000+00:002007-03-07T04:54:09.000+00:00Appologies in advance for the underscores - despit...Appologies in advance for the underscores - despite what the blog says HTML syntax isn't on!<br /><br />Using C3S (http://www.artima.com/weblogs/viewpost.jsp?thread=182412) the examples are:<br /><br />__public void sort( List< String > list, String selected ) {<br />____Collections.sort list, method( str1, str2 ) {<br />______if ( str1.equals selected ) {<br />________return -1<br />______} else if ( str2.equals selected ) {<br />________return 1<br />______} else {<br />________return str1.length - str2.length<br />______}<br />____}<br />__}<br /><br />and<br /><br />__final map = ...<br />__for ( final entry : map.entrySet ) {<br />____System.out.println entry.getKey + "=" + entry.getValue<br />__}Howard Lovatthttp://www.artima.com/weblogs/viewpost.jsp?thread=182412noreply@blogger.comtag:blogger.com,1999:blog-741750605858169835.post-90284653677158971142007-03-05T21:07:33.000+00:002007-03-05T21:07:33.000+00:00@Tom, Thanks for spotting the error, its fixed now...@Tom, Thanks for spotting the error, its fixed now. I agree that the BGGA example could be written as you show, I just think the ternary operator is less commonly used than an if statement. Effectively, this blog shows where BGGA might be considered a little weak (example 3) or strong (example 4).Stephen Colebournenoreply@blogger.comtag:blogger.com,1999:blog-741750605858169835.post-70228052894244983592007-03-05T19:59:59.000+00:002007-03-05T19:59:59.000+00:00I think your FCM example needs the "final&quo...I think your FCM example needs the "final" keyword removed from the param "selected"?<br /><br />And the BGGA example could say the following:<br /><br />Collections.sort(list, {String str1, String str2 =><br /> str1.equals(selected) ? -1 : str2.equals(selected) ? 1 : str1.length() - str2.length()<br /> });<br /><br />But you wouldn't always want to do this, and your example shows well what kinds of cases BGGA is optimized around. Specifically, giving return values for short expressions is easy. Longer blocks are best for voids. Interesting to consider, but I wouldn't call it the worst trade-off ever made.<br /><br />Thanks for the examples.Tom Palmernoreply@blogger.com