Extension methods
Posted: Sun Apr 06, 2008 9:03 pm
This feature is available in development.
You can now add methods to existing types, even ones from DLLs that you don't have the source for. This feature is similar to Smalltalk and Objective-C categories, and C# and VB's extension methods. Unlike C# and VB, you can use this feature even in .NET 2.0.
Here is an example taken from CobraLang.cobra which now enhances lists to have a "swap" convenience method:
So now you can say "someList.swap(i, j)". You can also extend classes and structs.
In addition to "enhancing" library classes, another use would be to organize your code. For example, you could add UI oriented methods to domain objects, but put them in extensions in a separate file. Then if you implement another form of UI (say WUI vs. GUI), you can easily exclude the old UI methods. I used that approach quite a bit when I was an Objective-C developer.
Note that the compiler will only "see" extension methods that are in your current namespace or brought in via "use" directives (as in "use Some.Name.Space").
The test cases for the extensions are found in <Workspace>/Tests/420-extensions
As with ordinary methods and classes, you can have "test" sections and contracts.
There are some limitations that will be addressed in the future:
-- Cannot effectively extend primitive types such as "int" and "bool".
-- Cannot extend generic types.
-- Cannot overload against a method in the extended type (but you can overload within the extension you are writing).
-- Cannot read extension methods found in .NET 3.5 standard libraries.
I would appreciate it if some of you would take this feature for a test drive and give feedback. Thanks.
You can now add methods to existing types, even ones from DLLs that you don't have the source for. This feature is similar to Smalltalk and Objective-C categories, and C# and VB's extension methods. Unlike C# and VB, you can use this feature even in .NET 2.0.
Here is an example taken from CobraLang.cobra which now enhances lists to have a "swap" convenience method:
extend System.Collections.IList
def swap(i as int, j as int)
temp = this[i]
this[i] = this[j]
this[j] = temp
So now you can say "someList.swap(i, j)". You can also extend classes and structs.
In addition to "enhancing" library classes, another use would be to organize your code. For example, you could add UI oriented methods to domain objects, but put them in extensions in a separate file. Then if you implement another form of UI (say WUI vs. GUI), you can easily exclude the old UI methods. I used that approach quite a bit when I was an Objective-C developer.
Note that the compiler will only "see" extension methods that are in your current namespace or brought in via "use" directives (as in "use Some.Name.Space").
The test cases for the extensions are found in <Workspace>/Tests/420-extensions
As with ordinary methods and classes, you can have "test" sections and contracts.
There are some limitations that will be addressed in the future:
-- Cannot effectively extend primitive types such as "int" and "bool".
-- Cannot extend generic types.
-- Cannot overload against a method in the extended type (but you can overload within the extension you are writing).
-- Cannot read extension methods found in .NET 3.5 standard libraries.
I would appreciate it if some of you would take this feature for a test drive and give feedback. Thanks.