4 ms·
I don't know Ruby or Lisp, but Python, at least, doesn't like it if you call methods on nil. Smalltalk and Objective C both let you do that (they do nothing and
by david-given 11y ago
I don't know Ruby or Lisp, but Python, at least, doesn't like it if you call methods on nil. Smalltalk and Objective C both let you do that (they do nothing and return nil).
- dragonwriter 11y agoIn Ruby, nil is the singleton object of NilClass, and you can call method on it like any object -- heck a key way of distinguishing it from False is that they respond differently to .nil?
- masklinn 11y ago> I don't know Ruby or Lisp, but Python, at least, doesn't like it if you call methods on nil. That's false. Python likes it every bit as much as if you call methods on any other object: it works fine if you wall a method which exists on the object, raises an AttributeError if you try to access an attribute which does not exist and raises a TypeError if you try to call an attribute which isn't callable. >>> None.__hash__() -9223372036578437293 > Smalltalk let you do that (they do nothing and return nil). That is also false. While UndefinedObject responds to a fair number of messages (compared to NoneType anyway), sending an "unknown" message to `nil` will signal an exception as it would just about anywhere else (because it'll defer to the standard Object implementation of #doesNotUnderstand). You can redefine `UndefinedObject doesNotUnderstand` so that it does something (or does nothing and return `nil`), but that definitely isn't the default behaviour, and it isn't generally considered a good idea.
- david-given 11y ago>> Smalltalk let you do that (they do nothing and return nil). > That is also false. Oops, you're absolutely right --- I was thinking of Objective C: http://stackoverflow.com/questions/156395/sending-a-message-to-nil http://stackoverflow.com/questions/156395/sending-a-message-...