This post originated from an RSS feed registered with Agile Buzz
by James Robertson.
Original Post: OO Principles in .NET
Feed Title: David Buck - Blog
Feed URL: http://www.cincomsmalltalk.com/rssBlog/buck-rss.xml
Feed Description: Smalltalk can do that
It's interesting to see how many Smalltalk OO techniques aren't transferrable to .NET. I know that most techniques involving blocks won't work directly. There are a lot of small techniques, however, that aren't transferrable.
There are times when you need to test the class of an object. In Smalltalk, you would use one of three techniques:
isMemberOf:
isKindOf:
implement an isSomething method in Object (returning false) and in your class (returning true)
In .NET, you can use GetType() and compare the result to the typeof(MyClass) to do the same as isMemberOf:. The isKindOf: test isn't so easy. There's a method for types called IsSubclassOf which tests whether a class is a subclass of another. Sadly, it excludes the class itself, so to implement isKindOf:, you'd have to say:
if ((object.GetType() == typeof(MyClass))
|| (object.GetType().IsSubclassOf(typeof(MyClass))))
Since you can't extend a system class (yet), there's no way to implement an InKindOf method in the proper class.
Now, consider the isSomething method approach. Since you can't implement a new method in Object, this technique has a limited usefulness.
How about conversion methods? Smalltalk's collections can convert themselves into other kinds using messages like asSet, asOrderedCollection, etc. Again, these methods don't exist in the .NET library and there's no way to add them.
I find the lack of extensions in .NET quite limiting. As I understand it, Microsoft is planning to add this feature. For now, we have to live with the limitations in the library.