Null Object works still fine

Adam D. Ruppe destructionator at gmail.com
Sat Aug 11 15:35:57 PDT 2012


On Saturday, 11 August 2012 at 22:23:12 UTC, Namespace wrote:
> This code works fine but it shouldn't, or?


The reason is the print method is private, which means it is also 
automatically final and doesn't access any member variables. 
Since it doesn't access members, it doesn't actually try to use 
the object and thus works ok.

(You can access private methods as long as you are still in the 
same module, so this is by design too.)

It being final is important because it means it is just a regular 
function - it doesn't need to access the object's virtual 
function table either.


If it was a public final method, it would say "AssertEerror: null 
this", but since it is private I think that check is 
intentionally left out.



But what you have here is everything working right, but the rule 
might seem weird... it's just because you aren't actually 
accessing the object, so the fact that it is null never comes 
into play.



More information about the Digitalmars-d-learn mailing list