"Peter Alexander" <peter.alexander...@gmail.com> wrote in message news:vicaibqyaerogseqs...@forum.dlang.org... > On Friday, 2 March 2012 at 04:53:02 UTC, Jonathan M Davis wrote: >> >> It's defined. The operating system protects you. You get a segfault on >> *nix and >> an access violation on Windows. > > False. > > ----------------------- > import std.stdio; > > class Foo > { > final void bar() > { > writeln("I'm null!"); > } > } > > void main() > { > Foo foo; > foo.bar(); > } > ----------------------- > > % dmd test.d -O -release -inline > % ./test > I'm null! > % > > ----------------------- > > You only get an error if there is a memory access involved (vtable, member > data etc.) >
Technically speaking, there is no dereference of null occurring there. It *looks* like there is because of the "foo.bar" notation, but remember, calling a member function is not really dereferencing. It's just sugar for: foo.vtable[index_of_bar](foo); Since "bar()" is a final member of "Foo" and "foo" is statically known to be type "Foo", the usual vtable indirection is unnecessary, so it reduces to: bar(foo); Passing null into a function doesn't involve dereferening the null, and bar() doesn't dereference "this", so there's never any dereferencing of null. Therefore the rule about null dereferences always being caught does still hold true. It is counter-intuitive, though.