Can call static method with null reference
Steven Schveighoffer
schveiguy at yahoo.com
Mon Jun 24 08:51:52 PDT 2013
On Sun, 23 Jun 2013 06:09:19 -0400, Jonathan M Davis <jmdavisProg at gmx.com>
wrote:
> On Sunday, June 23, 2013 12:02:42 Namespace wrote:
>> Also I don't know why I should call static methods from an
>> instance. What's the purpose?
>
> It's stupid and pointless as far as I can tell, but I believe that C++,
> Java,
> C#, and D all do it, so as stupid as it is, it's a common stupidity. I
> certainly wish that we could change it, but I wouldn't expect Walter to
> agree
> to the change, since it would break at least some existing code, and I
> suspect
> that he doesn't consider the fact that you can call static functions on
> instances to be a problem. That's not the sort of thing that he generally
> seems to think is an issue. It's almost always stuff that causes actual
> bugs
> that he agrees to change and not things that are aesthetically
> displeasing or
> which could theoretically cause bugs.
I actually had a bug caused by this.
But the reason is simply duck typing.
For example:
class InfiniteRange
{
...
static bool empty() { return false; }
}
My suggestion to fix this has always been: only allow static calls on
instance variables via opt-in. e.g.:
class InfiniteRange
{
static bool empty() { return false; }
alias InfiniteRange.empty this.empty; // just an example
}
This allows all the benefit, but none of the bug-prone problems.
-Steve
More information about the Digitalmars-d-learn
mailing list