Non-null objects, the Null Object pattern, and T.init

Michel Fortin michel.fortin at michelf.ca
Sun Jan 19 03:54:05 PST 2014


On 2014-01-19 11:30:00 +0000, "bearophile" <bearophileHUGS at lycos.com> said:

> Jacob Carlborg:
> 
>> I think nil (null) works quite nicely in Ruby. nil is a singleton 
>> instance of the NilClass class. Since it's an object you can call 
>> methods on it, like to_s, which returns an empty string. It works quite 
>> well when doing web development with Ruby on Rails. If you're trying to 
>> render something that's nil you'll get nothing, instead of crashing 
>> that whole page. Sure, there might be a small icon or similar that 
>> isn't rendered but that's usually a minor detail. If the page is 
>> working and main content is rendered that's preferable.
> 
> Walter is arguing against this solution in D.
> While that can be OK for Ruby used for web development, for a 
> statically typed language meant for safe coding styles, there are 
> better type-based solution to that problem.

It won't work in D's type system anyway. Ruby is dynamically typed, 
that's why it can work.

Interestingly, in Objective-C calling a method on a null object pointer 
just does nothing. That's a feature that is often useful when chaining 
calls that might return null (you don't have to do all these extra 
checks) or with weak pointers, but it can on occasion lead to subtle 
bugs.

-- 
Michel Fortin
michel.fortin at michelf.ca
http://michelf.ca



More information about the Digitalmars-d mailing list