September 14, 2005
We discussed a while ago external methods in D:

  Currently  function void foo(char[] str, int p);
  can be called as
      char s[];
      s.foo(12);
  which is clearly external method notation.
  'foo' can play a role of an external method for type char[].


In the new draft of C# (v 3.0) following appeared:

26.1.1 Declaring extension methods

Extension methods are declared by specifying the keyword this as a modifier
on the first parameter of the methods. Extension methods can only be
declared in static classes. The following is an example of a static class
that declares two extension methods:
namespace Acme.Utilities
{
      public static class Extensions
      {
            public static int ToInt32(this string s) {
                  return Int32.Parse(s);
            }
            public static T[] Slice<T>(this T[] source, int index, int
count) {
                  if (index < 0 || count < 0 || source.Length - index <
count)
                       throw new ArgumentException();
                  T[] result = new T[count];
                  Array.Copy(source, index, result, 0, count);
                  return result;
            }
      }
}
Extension methods have all the capabilities of regular static methods.
In addition, once imported, extension methods can be invoked
using instance method syntax.

URL to proposed C# 3.0 specification
http://download.microsoft.com/download/9/5/0/9503e33e-fde6-4aed-b5d0-ffe749822f1b/csharp
3.0 specification.doc
(url contains whitespaces)

Just for your informaticon.

Andrew Fedoniouk.
http://terrainformatica.com


September 14, 2005
"Andrew Fedoniouk" <news@terrainformatica.com> wrote in message news:dg9t9p$1sli$1@digitaldaemon.com...
> We discussed a while ago external methods in D:
>
>  Currently  function void foo(char[] str, int p);
>  can be called as
>      char s[];
>      s.foo(12);
>  which is clearly external method notation.
>  'foo' can play a role of an external method for type char[].
>
>
> In the new draft of C# (v 3.0) following appeared:
>
> 26.1.1 Declaring extension methods
>
> Extension methods are declared by specifying the keyword this as a
> modifier on the first parameter of the methods. Extension methods can only
> be declared in static classes. The following is an example of a static
> class that declares two extension methods:
> namespace Acme.Utilities
> {
>      public static class Extensions
>      {
>            public static int ToInt32(this string s) {
>                  return Int32.Parse(s);
>            }
>            public static T[] Slice<T>(this T[] source, int index, int
> count) {
>                  if (index < 0 || count < 0 || source.Length - index <
> count)
>                       throw new ArgumentException();
>                  T[] result = new T[count];
>                  Array.Copy(source, index, result, 0, count);
>                  return result;
>            }
>      }
> }
> Extension methods have all the capabilities of regular static methods.
> In addition, once imported, extension methods can be invoked
> using instance method syntax.
>
> URL to proposed C# 3.0 specification
> http://download.microsoft.com/download/9/5/0/9503e33e-fde6-4aed-b5d0-ffe749822f1b/csharp
> 3.0 specification.doc
> (url contains whitespaces)
>
> Just for your informaticon.
>
> Andrew Fedoniouk.
> http://terrainformatica.com

I like this syntax.  I'd also be nice for using toString() in templates, as it's a member function for classes and a regular function for atomic types; if toString() were defined as "char[] toString(this int x)", we could then just always use the external syntax in templates.