NULL, Nil and nil

Wow!!!!

I haven’t have curiosity in the difference of NULL, Nil and nil once I got to know their differences. About 10 years ago, people asked me and others why there are those threes looking similar or even same. So, I and others explained about their differences at http://www.osxdev.org. As far as I remember they were defined like these :

  • NULL : 0
  • Nil : (Class) 0
  • nil : (id)0

In that the values are 0s, they are the same. But the compiler checks types. So, the compiler complaint they were different.
Why are they treated differently? NULL is purely redefined 0. It’s a value. However, id is like (void *). So, although it’s 0, it is a pointer pointing at address 0. (Class) is a Class pointer. However, even before that time, I remember that they were all defined as 0. I believe it was due to C standard at that time. You could assign a pointer variable to any value. It was thought to be powerful for C programmers, but for younger generation of programmers who studied more type-safe languages, it was considered dangerous.

Fast forward….

I found out that I couldn’t search nil or Nil by option-clicking them with Xcode 4.5 GM. It was possible with Xcode 3.x and probably earlier versions of Xcode 4. So, I manually searched them and found this.

#ifndef Nil
# if __has_feature(cxx_nullptr)
#   define Nil nullptr
# else
#   define Nil __DARWIN_NULL
# endif
#endif

#ifndef nil
# if __has_feature(cxx_nullptr)
#   define nil nullptr
# else
#   define nil __DARWIN_NULL
# endif
#endif

#ifndef __DARWIN_NULL
#define __DARWIN_NULL NULL
#endif

So, it was changed again. I don’t know from when they were changed, but OS X 10.7 SDK has the new definition. Probably it was changed then in 10.6. I believe the last time I looked up them was either in the days of 10.4 or 10.5.

I was shocked that I could be seen as a novice in Objective-C programming to young Obj-C programmers. There are things like that you checked something many years ago, and there is no difference semantically currently and so didn’t check it again. But I often found out that young interviewers asked me very small details and if I answered differently from what they knew, they treated me as a person who doesn’t know about it, even when what I know was still valid, but only small detail was changed without affecting semantics.

This is dangerous.

2 responses to this post.

  1. Posted by Dmitry on February 15, 2013 at 12:07 AM

    You would be surprised by the fact that fancy nullptr version is not used in objective-c++ files. Set C++11, and try that in mm file:

    NSUInteger a = nil;
    NSUInteger b = nullptr;

    First one will succeed, second will fail. Magic.

    Reply

Leave a reply to Dmitry Cancel reply