Overloading static methods

Steven Schveighoffer schveiguy at yahoo.com
Mon Aug 29 07:44:44 PDT 2011


On Mon, 29 Aug 2011 10:25:18 -0400, Jacob Carlborg <doob at me.com> wrote:

> On 2011-08-29 16:10, Daniel Murphy wrote:
>> "Jacob Carlborg"<doob at me.com>  wrote in message
>> news:j3fi1u$1uge$1 at digitalmars.com...
>>> I just got and idea, what about allowing to overload methods based on  
>>> if
>>> they're static or not.
>>
>>  From my list of 'Andrei' bugs:
>> http://d.puremagic.com/issues/show_bug.cgi?id=3345
>>
>> It sounds like a good idea, but what are the real use cases and what  
>> are the
>> corner cases?
>
> In my serialization library, Orange, I want to have a "reset" method  
> that is both static and non-static. The static method would reset some  
> static variables while the non-static method would reset the instance  
> variables.
>
> Another use case is to have a static and non-static opDispatch at the  
> same time. I've been playing around with the idea of having something  
> similar to Ruby on Rails' activerecord implemented in D. In activerecord  
> you can do something like this:
>
> p = Person.find_by_name("Joe")
> name = p.name
>
> Both "find_by_name" and "name" are implemented using "method_missing"  
> (Ruby's opDispatch). In D, this would require both a static and a  
> non-static opDispatch.

opDispatch is a much more convincing use case than just some random  
function name.

But one function name can be solved.  i.e. opStaticDispatch.

I had a similar issue with D a long long time ago, but I since fixed the  
problem by just renaming one of the functions.

My opinion is that static methods should *not* be callable from an  
instance, you should need typeof(instance).staticMethod.  The current  
allowance is misleading.

This should solve some of the issues, but of course, you'd need to allow  
overloading of the method name in static and non-static forms.

-Steve


More information about the Digitalmars-d mailing list