Walter: extend existing classes with new methods?

Bill Baxter wbaxter at gmail.com
Thu Aug 31 05:41:40 PDT 2006


Ivan Senji wrote:
> Bill Baxter wrote:
> 
>> Marcio wrote:
>>
>>> Walter,
>>>
>>>    Do you plan to add the ability of adding methods to an existing 
>>> class, from another module?

>> compile, or it successully calls foo() at runtime.  There is no 
>> runtime check to see if the method exists or not before calling it.  
>> What you're asking for is a really big fundamental change to what D is.
> 
> 
> Not really. It is possible to extend a type without the need for runtime 
> checks. C# 3.0 has that feature and it is called extension methods.
> Here is a little example:
> 
> static class Program
> {
>     static void Main(string[] args)
>     {
>         string s = "Hello, world";
>         s.Print();
>     }
> 
>     static void Print(this string s)
>     {
>         Console.WriteLine(s);
>     }
> }
> 
> There is nothing dynamic about that.

I see. Is that little bit of syntactic sugar all the original poster was 
asking for?  And was that also all Tim Sweeny was asking for in the 
referenced presentation?   That means you can only add non-virtual 
methods, right?

--bb



More information about the Digitalmars-d mailing list