digitalmars.D - Back to external methods form C# 3.0 point of view
- Andrew Fedoniouk (41/41) Sep 14 2005 We discussed a while ago external methods in D:
- Jarrett Billingsley (6/47) Sep 14 2005 I like this syntax. I'd also be nice for using toString() in templates,...
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[]. 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. 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
Sep 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[]. 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. 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.comI 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.
Sep 14 2005