Ticket #1 (accepted enhancement)

Opened 14 years ago

Last modified 14 years ago

Extension methods and properties

Reported by: Chuck.Esterbrook Owned by: Chuck
Priority: major Milestone: Cobra 0.9
Component: Cobra Compiler Version:
Keywords: Cc:


Support extension methods and properties on existing classes, interfaces and structs whether they come from DLLs or declarations.

This feature is found in Smalltalk, Objective-C (categories), C#, VB and others. C# and VB do it on .NET 3.5, but Cobra is still targeting 2.0 and will support it there. The syntax will be:

extend Foo

def bar


get baz as int

return 1

One interesting question is whether or not to allow a nil receiver. C# does which, imo, is quite strange. For "obj.Foo()" in C# you need to know if Foo() is declared in obj's type or outside of it to know whether or not you can pass nil.

Change History

Changed 14 years ago by Chuck

  • status changed from new to accepted

Changed 14 years ago by Chuck

  • component changed from component1 to Cobra Compiler

This was partially done in  Cobra 0.8.

Changed 14 years ago by Chuck

  • type changed from defect to enhancement

Change to enhancement.

Changed 14 years ago by Chuck

AFAIK what's missing is:

  • Extensions X method overloads
  • Extensions on generics

Other mind-benders may include:

  • Extensions on method overloads in a generic type
  • Generic extension methods (not to be confused with non-generic extension methods on generic types)
  • Generic extension methods on method overloads in a generic type

Also of note, it's no longer clear whether or not extension properties are desirable. Cobra already allows "" for invoking a method with no args, and it's not as if property-centric classes such as DataGridView are going to pick up on extension properties.

Changed 14 years ago by Chuck

Here is code for an extended, overloaded method:

extend String

	def split(chars as IList<of char>) as String[]
		return String[](5) # TODO

class X

	def main is shared
		s = 'a,b:c:d,e,f'
		trace s.split([c',', c':'])

This fails during code generation. But it may be failing due to a bug in _bindImp.

Changed 14 years ago by Chuck

Also, support shared extension methods:

extend String
    def foo is shared
# ...

Changed 14 years ago by Chuck

  • milestone set to Cobra 0.9

Changed 14 years ago by Chuck

  • Method overloads in extensions were done some time ago.
  • Generics were done today in changeset:1853

There is still more testing and investigation to be done before this ticket can be closed. Please report any problems here or in the discussion forums.

Changed 14 years ago by gauthier

well done for shared extension methods :)

I think support for c# 3 extension methods is a must-have for next milestone (if I'm right, it's simple static method with an attribute for the first parameter), this would also bring nearer the linq goodness with IEnumerable...

Changed 14 years ago by Chuck

It's true we need to support C# 3 extension methods at some point, but currently we're on .NET 2.0 and I don't think we would see such things unless we moved to .NET 3.5. So I think before that we need to get a cobra-n20.exe and cobra-n35.exe going. Make sense?

Note: See TracTickets for help on using tickets.