Page 1 of 1

Extend generic types

PostPosted: Tue Dec 30, 2008 2:22 am
by Charles
I have implemented generic type extensions. That is, you can now "extend" on a generic type such as IList<of T>.

Quick example:
extend Dictionary<of TKey, TValue>

def lastValue as TValue
keys = List<of TKey>(.keys)
keys.sort
return this[keys[keys.count-1]]


class P

def main is shared

d = {
'a': 1,
'b': 2,
'z': 3,
'c': 4,
'd': 5,
}

assert d.lastValue == 3


You can browse the test cases in your local workspace or online at http://cobra-language.com/trac/cobra/browser/cobra/trunk/Tests/420-extensions/400-extend-generics.

This feature will enable us to add convenience methods to IList, IDictionary, etc.

If you find any problems, please report them here.

Re: Extend generic types

PostPosted: Tue Dec 30, 2008 11:43 am
by gauthier
that's looking handy :)

as for .net extension methods, do you plan to support C# 3 extension method call and definition soon?

def transform(this text as String) as String is shared
return text + text

Re: Extend generic types

PostPosted: Tue Dec 30, 2008 9:15 pm
by Charles
If you mean syntaxwise, no. The Cobra syntax is already established as:
extend String
def transform as String
return this + this

If you mean translating Cobra's extensions down to the C# syntax so that C# 3 programs see them properly, yes we would do that as part of supporting .NET 3.5+. Same with reading C#/VB extensions from .DLLs.

But right now Cobra is still on .NET 2.0 and I haven't given much thought to moving to later versions of .NET.