This post originated from an RSS feed registered with Agile Buzz
by James Robertson.
Original Post: Transient Collections
Feed Title: David Buck - Blog
Feed URL: http://www.cincomsmalltalk.com/rssBlog/buck-rss.xml
Feed Description: Smalltalk can do that
Something that I find interesting about the Collections classes in Smalltalk compared to C# or Java, is that Smalltalk collections often tend to be treated as transient objects which are never assigned to variables. For example, consider the following code snippet:
commands reverse do: [:each | each undo]
This is possible because the reverse method copies the collection and returns the reversed collection. In C#, there is a Reverse method, but it acts destructively on a collection and has no return value. The corresponding C# code would be:
There's actually no reason why you couldn't define a Reverse method for C# which is non-destructive and returns the new collection, but Microsoft simply didn't provide it. They don't seem to see that collections may be used transiently and you may not want to have to assign them to a variable.
In Smalltalk you see this technique used frequently with select: collect: reject: and so forth. These methods take a block and return a collection which non-destructively derives from the original. These can't currently be implemented in C# because it has no lexical closures (blocks). The next version of C#, however, will have anonymous methods and should be able to support this kind of operation. I wonder if the Microsoft view of collections will change when this becomes possible.