Can call static method with null reference

Jonathan M Davis jmdavisProg at gmx.com
Sun Jun 23 03:09:19 PDT 2013


On Sunday, June 23, 2013 12:02:42 Namespace wrote:
> > I don't see what's so terrible about it
> 
> It's bug prone.
> 
> class Foo {
> public:
> 	static void test1() { }
> 	void test2() { }
> }
> 
> Foo f;
> f.test1(); /// Oh nice, that works, f is not null.
> f.test2(); /// WTF? f is null?

I fail to see what's bug-prone about that. It's confusing, but it's not 
causing any bugs.

> 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.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list