Live data from Hacker News

nil, Nil, NULL, NSNull

nshipster.com

41–50 of 66 posts

Re: nil, Nil, NULL, NSNull

#41

Any use cases for Nil, that is, the class pointer to nothing? I've not seen it in the wild (which isn't saying a great deal.)

You could use it to test whether or not you really got a class from NSClassFromString(). For instance: Class _myClass; // aClassName was previously defined if((_myClass = NSClassFromString(aClassName)) == Nil) [NSException raise:@"InvalidClass" reason:@"Unknown class named %@.", aClassName); id myObj = [[_myClass alloc] init]; [myObj whatever];

Makes sense, thanks.

Re: nil, Nil, NULL, NSNull

#42
post #2

Good article overall. However, I must object to this bit: "C represents nothing as 0 for primitive value" That's not true. There is no "nothing" for integer types, and floats either also lack "nothing", or have NaN as their "nothing", depending on your perspective. One of the (many) annoying features of C-style NULL is that it means that some types are effectively option types while other types aren't, and there's no…

Definitely spot on regarding 0 not representing 'nothing'. However, 'int *' is inherently "pointer to int or nothing" (in the same style as your NSString example), so I'm not sure I agree with your assertion that some types are option types while some aren't in C. Any type can be an option type (in your sense of the term, again a la your NSString example) by making it into a pointer. Similarly, using an NSString with…

int and pointer to int are two different types. Pointer-to-int being an option type does not make int an option type. So there are still option types and non-option types.

(Using the prevailing terminology. I'm not sure I'm comfortable with this use of the term "option type", but I'm rolling with it for now.)

Re: nil, Nil, NULL, NSNull

#43
post #42

Earlier quoted context omitted.

Definitely spot on regarding 0 not representing 'nothing'. However, 'int *' is inherently "pointer to int or nothing" (in the same style as your NSString example), so I'm not sure I agree with your assertion that some types are option types while some aren't in C. Any type can be an option type (in your sense of the term, again a la your NSString example) by making it into a pointer. Similarly, using an NSString with…

int and pointer to int are two different types. Pointer-to-int being an option type does not make int an option type. So there are still option types and non-option types. (Using the prevailing terminology. I'm not sure I'm comfortable with this use of the term "option type", but I'm rolling with it for now.)

Precisely. It's not the case that "any type can be an option type" as shock-value stated. Rather, in C, all pointer types are option types whether you want them to be or not, and all other types are not.

Re: nil, Nil, NULL, NSNull

#44
post #19

It's worth noting that Go has interesting handling for empty values as well. A nil basically means "the zero value for a given data type". Thus, an empty array compares equal to nil: http://tour.golang.org/#36

That is incorrect. Certain types can be nil, and those types have a zero value of nil, but nil is not equal to the zero value for all types. You don't have a "empty array" there, you have an uninitialized slice, with no corresponding array at all, empty or otherwise. Uninitialized slices are nil, but that is not generally true, only for the specified types. If you initialize it, even to an actual "0 array" (as close as we can get), it ceases to be nil. See http://golang.org/ref/spec#The_zero_value , and w.r.t. the tour code, switch it to:

    package main

    import "fmt"

    func main() {
    	var z []int = make([]int, 0, 0) // jerf changed this line
    	fmt.Println(z, len(z), cap(z))
    	if z == nil {
    		fmt.Println("nil!")
    	} else {
    		fmt.Println("initialized slice is not nil")
    	}
    	
    	// This won't even compile; "cannot convert nil
    	// to type int" at the if clause:
    	// var a int = 0
    	// if a == nil {
    	//	fmt.Println("jerf is wrong; nil does == 0")
    	// }
    }
(Oh, and for those that don't know, on the other end of that tour link is the live Go tutorial, which allows you to run arbitrary Go code in your browser. You can fiddle with this live, with no installation of Go.)

Re: nil, Nil, NULL, NSNull

#45
post #35
post #31

Earlier quoted context omitted.

This isn't a good idea since even it happens to work it invokes undefined behaviour. If you change the method to virtual it will crash. I think you mean non-virtual method, not 'class method' or static method which wouldn't have a this pointer.

That's a good point, calling a virtual method on a null pointer is the source of some backtraces that seem to start a few bytes past 0. Naming things is always difficult, but I meant class method, although what I should have said was non-virtual class method, which is possible in C++. Both virtual and non-virtual have "this" pointers, the difference is whether the class method to call is looked up at runtime or compi…

I believe the correct terminology for C++ would be "member function", and specifically "non-virtual member function" for the ones that actually work with NULL. I also wouldn't be surprised if using a non-virtual member function with NULL is actually undefined behavior that just happens to work with popular compilers, although I'm too lazy to look up the standard to see.

"Class method" is usually used to refer to methods that class objects have themselves, as opposed to an "instance method", which is a method that instances of a class possess. C++ doesn't have class objects or class methods.

Re: nil, Nil, NULL, NSNull

#46
post #15

Earlier quoted context omitted.

You do raise an interesting point that a nil swallowing language forces you to write your methods in such a way as to logically return false for a nil receiver. I'd argue this is the way most methods are naturally written, so it doesn't have too much of an impact. I'm struggling to think of an example where logically returning true for a nil receiver is a much more natural than the other way around. Most of them, inc…

`[a compare:b]` returns 0 both if they are equal and if a is nil.

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

#47
post #25

Earlier quoted context omitted.

To clarify: I mean to say that 0 represents "nothing" in C in the same way that it does in math. This is one of the things I love about C--the ability to exploit the properties of numbers directly, rather than needing to build abstractions around them. Bitmasks, for example, are one of my favorite things ever. But yeah, point well-taken. Perhaps I'll take the opportunity to wax on the other special values used in Fou…

0 isn't really "nothing", though. It's special, in that it's the additive identity of a lot of useful sets of numbers, but it's definitely something. An integer containing 0 is completely different from a pointer containing nil, conceptually speaking. To really have "nothing", you could use an int*, where a NULL pointer means "nothing", and a valid pointer means it contains a value. Or something more efficient with e…

[deleted]

Re: nil, Nil, NULL, NSNull

#48
post #11

Nice article, but I just wanted to throw out that if being able to call a class method on a null object in C++ is desirable, you can actually check for that condition instead of just crashing. if(!this) return false; // Or whatever Of course it's open for debate as to whether this is something to encourage, but it can help from having to use a Null Object pattern in some situations.

No, you really can't do this. If you think you can, tell me what you think the output of this program will be:

  class X
  {
  	int a;
  public:
  	X() : a(123) { }
  	int get_a() { return this ? a : 1; }
  };
  
  class Y
  {
  	int b;
  public:
  	Y() : b(456) { }
  	int get_b() { return this ? b : 2; }
  };
  
  class Z : public X, public Y
  {
  	int c;
  public:
  	Z() : c(789) { }
  	int get_c() { return this ? c : 3; }
  };
  
  #include 
  int main()
  {
  	Z *z = NULL;
  	std::cout get_a() get_b() get_c() 

Re: nil, Nil, NULL, NSNull

#49
post #25

Earlier quoted context omitted.

To clarify: I mean to say that 0 represents "nothing" in C in the same way that it does in math. This is one of the things I love about C--the ability to exploit the properties of numbers directly, rather than needing to build abstractions around them. Bitmasks, for example, are one of my favorite things ever. But yeah, point well-taken. Perhaps I'll take the opportunity to wax on the other special values used in Fou…

0 isn't really "nothing", though. It's special, in that it's the additive identity of a lot of useful sets of numbers, but it's definitely something. An integer containing 0 is completely different from a pointer containing nil, conceptually speaking. To really have "nothing", you could use an int*, where a NULL pointer means "nothing", and a valid pointer means it contains a value. Or something more efficient with e…

Step back for a second and think about what the number 0 means. If I give you 0 dollars, what have I given you?

Or go to Wikipedia: 'The wThe word zero came via French zéro from Venetian zero, which (together with cypher) came via Italian zefiro from Arabic صفر, ṣafira = "it was empty", ṣifr = "zero", "nothing". ord zero came via French zéro from Venetian zero, which (together with cypher) came via Italian zefiro from Arabic صفر, ṣafira = "it was empty", ṣifr = "zero", "nothing".'

Re: nil, Nil, NULL, NSNull

#50
post #15

Earlier quoted context omitted.

`[a compare:b]` returns 0 both if they are equal and if a is nil.

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. Obv…

judofyr's code is actually valid. Most "value" classes have compare: — NSString, NSDate, NSNumber, etc.
Post reply on HN