News for hackers. Stuff that matters.
Re:True, False And Nil Objects In Ruby by stcredzero on Tuesday September 08, 2009 @09:36AM (#810917)
It's a bad idea to put methods on nil willy-nilly. I see stuff like this in Smalltalk projects all the time. People like to put < and > on nil and think they're clever because they fixed their little routine that does a sort. What they don't realize, is that they've possibly broken something else that depends on the Does Not Understand exception as part of its normal operation. (Yes, remember there's only one nil object, and you have no comprehensive list of what implicit contracts it has to fulfill in the rest of the code base. Certain proxy mechanisms can break. I know of another mechanism in one Smalltalk's streams that would break as well.)

The correct way to replace your nil checks with polymorphism is with the MissingObject pattern. Languages with explicit types should give you this for free. I think that even duck-typed languages should do this. (Every time you define a class Foo, you get MissingFoo automatically.) They can often be provided by libraries.

Re:True, False And Nil Objects In Ruby by raganwald on Tuesday September 08, 2009 @11:16AM (#811157)
"The correct way to replace your nil checks with polymorphism is with the MissingObject pattern."

I strongly agree. Adding stuff to nil can have unintended consequences:

http://github.com/raganwald/homoiconic/blob/master/2009-02-0...

Re:True, False And Nil Objects In Ruby by jerf on Tuesday September 08, 2009 @11:09AM (#811139)
Another option that I'm really warming to is to put it right in the type system whether or not you might have "Nulls". You can sort [Int] and you can sort [Maybe Int] if you provide the correct sort functions, but either way, there's no surprises. There simply will not be a "null" in an [Int].

While I'm using Haskell's notation, it seems likely that pretty much any strongly typed language could do something like this, at least ad hoc. (That is, Haskell hasn't actually written this into the typing system, it's just a direct use of the type system that it already had. But even a C++-like language could probably put this in there if it was written that way from day one; arguably, it's allowing the nulls to exist in the first place that is actually the hack.)

Re:True, False And Nil Objects In Ruby by masklinn on Tuesday September 08, 2009 @12:19PM (#811298)
> it seems likely that pretty much any strongly typed language could do something like this

No. Or more precisely, in a dynamically typed language (which may or may not be strongly typed) there's no point in doing so, it won't buy you anything expressivity or correctness over nullable references.

Pretty much any statically typed language would benefit from making names/references non-nullable by default and moving nullability into the type system yes.

> it's allowing the nulls to exist in the first place that is actually the hack

How is it a hack? It's trivial to implement, but there's nothing hacky about it.

Re:True, False And Nil Objects In Ruby by jerf on Tuesday September 08, 2009 @06:33PM (#812115)
Type system hack, not code hack. Certainly the implementation is trivial, yes. Even under the weaker type systems of C++ or Java, having this magical, undeclared value that is also a Circle and a Car and an Account and so on is pretty dirty from the type point of view.

Haskell of course has its own troubles with bottom, and I note some people think we should be doing away with that, too.

Re:True, False And Nil Objects In Ruby by masklinn on Wednesday September 09, 2009 @07:33AM (#812938)
> Type system hack, not code hack.

It's not either. Types are simply implicitly defined as nullable, as they are in SQL.

Re:True, False And Nil Objects In Ruby by jerf on Wednesday September 09, 2009 @02:35PM (#813724)
And I call that a hack, especially when I don't think it was done on purpose or with thought, but simply because "that's how it needs to work". The fact that it wasn't intended as a hack doesn't make it not a hack; I'm sure that by 2050 we'll be looking back on types that could be magically NULL in much the same way I currently curse at SQL every time something generates "SELECT * FROM blah WHERE value IN ()". ("Damn it, SQL, it's just an empty list and always false, what's the damn big deal?! So very 70s...") There's a whole slew of things like that from older technologies; someday I really ought to make a list.
Re:True, False And Nil Objects In Ruby by mrduncan on Tuesday September 08, 2009 @10:07AM (#810994)
This is a classic case of "just because you can, doesn't mean you should".
Re:True, False And Nil Objects In Ruby by stcredzero on Tuesday September 08, 2009 @10:29AM (#811057)
It's not just that. One should replace conditionals with polymorphism. It's just that, in this case, not enough thought is given to how. Namely -- how can I accomplish this without possibly breaking lots of other things?