Can call static method with null reference

Jonathan M Davis jmdavisProg at gmx.com
Thu Jun 20 12:46:30 PDT 2013


On Thursday, June 20, 2013 21:38:57 Namespace wrote:
> Yes that's obvious. My question is: is that intended? IMO this
> could cause bugs.

It's a natural result of how the implementation works. Checking for null would 
just be extra overhead (Walter won't even do that for virtual functions which 
_will_ blow up when you call them on null references). And you'll get a 
segfault as soon as you actually use a member variable or virtual function 
with a null reference within a non-virtual function. So, I don't see why it 
would be a problem other than the fact that it's potentially confusing to 
people when they see a null this reference, since most people don't think 
that's possible.

And there's _definitely_ no point in checking if you're calling a static 
function. The only possible bug that you have there is if thought that you 
were calling a member function rather than a static function and that caused 
you to misunderstand what the code was doing and potentially write buggy stuff 
around it - but that's a problem caused by the fact that static functions can 
be called via an instance, and fixing that would mean making it illegal to call 
static functions on instances (which I would love to have happen but don't 
expect to ever happen).

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list