| Joe Walnes |
|
|||||||||||||||||||||||||||
|
|
|
|
||||||||||||||||||||||||||
|
The power of closures in C# 2.0Martin Fowler (obligitary Fowlbot namedrop) recently blogged about the power of closures in languages that support them. It's worth remembering that C# 2.0 has true closure support in the form of anonymous delegates. This includes reading and modifying variables outside the context of the closure - unlike Java's anonymous inner classes. Just for kicks, I've rewritten all of the examples Martin's Ruby examples in C# 2.0. This makes use of the improved APIs in .NET 2.0 pointed out by Zohar.
The code difference between the languages isn't that difference. The C# 2.0 code is obviously longer (though not a lot) because:
You can try this stuff out yourself by playing with Visual C# Express.
CommentsObieCool, but what's with the capitalized method names? ICK!!! Joe WalnesHere is a sample comparison of anonymous delegates in C# 2.0 with anonymous inner classes in Java. C# 2.0 int total = 0; Java final int[] total = { 0 }; This is something I end up doing a lot, and I cringe everytime. The anonymous bloat I don't mind so much, but the final array hack is what really gets me. This is necessary because any local variable outside the scope the of the inner must be final, so the only way to write to it is to use an array. Thankfully IDEA always automagically sorts this out for me, but it's still confusing for someone who is later trying to understand the code. verbatI said many times: c# 2.0 is a kind of statically typed ruby. Even the icollection apis wich gets anonimous delegates as parameters really resemble ruby's Enumerable. Pavel TavodaIf you want use scripting languages, go with Groovy. This have very extended closure support. Meitsi"The C# 2.0 code is obviously longer (though not a lot) because: C# 2.0 is staticly typed (let's not get started on the static vs dynamic debate)." No, not quite the right reason. C# is longer specifically because it uses manifest typing. Eric HodelEeee! The code runs all over the sidebar. Unreadable even at 1024 px width! Try putting such code blocks in a div with the style "overflow:auto" set Yuriyjust to remind that JScript supports this technique small sample: var A(1); The example code is wrong, I think. Shouldn't the FindAll example actually be:
Bill WoodI've put boo versions of these examples here: Boo is a new object oriented statically typed programming language for the Common Language Infrastructure with a python inspired syntax and a special focus on language and compiler extensibility. Bill WoodSorry the second link above to boo should be: JayI would like to check this out, but I can't read the C# because it drools out into the right sidebar. David VallnerMinor note by a rubyist - the Ruby code examples don't make use of omitting the return keyword. If you used return inside a closure, it would return from the method it was defined in. ChrisPlease God fix the messed up tables created by SiteMash and UnmoveableType so I can read this example. Thomas HafnerQuote: Static typing is a bad excuse for longer code. It's not obvious at f x = (\y -> x + y) It's a closure (see ). Nevertheless Haskel is a statically typed language (see table "Type Regards Thomas HafnerSorry that I've to post it again, but the Blog software skipped the Quote: Static typing is a bad excuse for longer code. It's not obvious at f x = (\y -> x + y) It's a closure (see http://www.haskell.org/hawiki/Closure). Nevertheless Haskell is a statically typed language (see table "Type Regards EricEssential C# 2.0 |
|
||||||||||||||||||||||||||
|
[RSS | RDF] © 2001-2004, Joe Walnes |
Powered by SiteMesh and Moveable Type. | |||||||||||||||||||||||||||