There were some great comments on my discussion of null propagation in object-oriented languages, and how it would add a nice parallel to the way XPaths are evaluated. I wanted to discuss them a bit more.
John Cowan pointed out you couldn't just annotate a method to say "if this method is applied to a null, return null", because there are polymorphism issues that arise from that. I agree.
He also noted that you could raise an exception if certain methods try to return null, to stop the null result from having a further method applied to it. To my mind, that would actually be more work than just handling the null pointer exceptions, without gaining you anything.
Steven Lott suggested that the trick was to avoid nulls. If a method doesn't have a result, return a singleton "no result" object rather than a null. That avoids the null pointer exception issue. This is an interesting solution, and certainly completely implementable in the here-and-now. The only practical issue that I see is that it requires you to define a suitable "no result" singleton object for every class that needs null propagation, so that the result you return has the appropriate methods. The singleton objects would also need to be programmed specially so that they only return a "no result" object when a method is called on them. That said, if you are generating your code, this wouldn't be too hard to implement.
In retrospect, the behaviour I would like to see is slightly different again. It would only apply when you have layered method calls, e.g.
E result = a.b().c().d().e();
The idea would be that if a method is marked as a "null propagation" method, and its result is null, the sequence of method calls is be stopped immediately and a null is returned. So if method "c" returned a null, methods "d" and "e" would never be executed, and "result" would be set to null.
However, the code
C intermediate = a.b().c(); E result = intermediate.d().e();
would cause a null pointer exception (the value of "intermediate" is null) because null propagation doesn't apply if you store a value in a variable and then apply methods to it in a later statement.
Any reasons why this wouldn't work?