News for hackers. Stuff that matters.
Re:nil, Nil, NULL, NSNull by judofyr on Monday January 07, 2013 @12:26PM (#5022030)
`[a compare:b]` returns 0 both if they are equal and if a is nil.
Re:nil, Nil, NULL, NSNull by ajanuary on Monday January 07, 2013 @01:33PM (#5022385)
Cheers for the counter-example.

I can't think of a compare method in the Objective-C core lib (the biggest nil-swallowing lib I know of). Does anyone know of one? Would be interesting to see how it's handled and if it causes issues.

You could return an enum, which would allow you to use non-obvious values (Lower = 1, Equal = 2, Higher = 3), though this would make it a bit of a pain to interface with C-style libs.

Obviously the presence of a work-around doesn't mean nil-swallowing is a good idea, but rather cements the idea that library authors need to put in extra effort to make sure nil receivers are handled appropriately.

Re:nil, Nil, NULL, NSNull by judofyr on Monday January 07, 2013 @01:59PM (#5022510)
> I can't think of a compare method in the Objective-C core lib (the biggest nil-swallowing lib I know of). Does anyone know of one? Would be interesting to see how it's handled and if it causes issues.

Maybe I misunderstood you, but there is plenty of compare:'s in Cocoa: http://cl.ly/image/1a112f1B1W1g.

They all return an enum that's either -1, 0 or 1.

Re:nil, Nil, NULL, NSNull by chc on Monday January 07, 2013 @03:43PM (#5023128)
judofyr's code is actually valid. Most "value" classes have compare: — NSString, NSDate, NSNumber, etc.
Re:nil, Nil, NULL, NSNull by Groxx on Monday January 07, 2013 @03:17PM (#5022972)
I'd argue this is correct. If a is nil, it's not comparable. The alternative would be to throw an exception, which is pretty simple to emulate by checking.

Also, nils aren't allowed in the collection classes, so sorting with compare: will simply never encounter this case.

Re:nil, Nil, NULL, NSNull by tyler-dodge on Monday January 07, 2013 @12:32PM (#5022055)
True, although none of the other return values make that much sense with nil either.