3 ms·
Is he really suggesting to replace all attempts to dereference null with no-ops? Weird. I think there are two ways to deal with this. Either it is known at com
by fauigerzigerk 17y ago
Is he really suggesting to replace all attempts to dereference null with no-ops? Weird.
I think there are two ways to deal with this. Either it is known at compile time that a particular variable must never be null. In that case the compiler should catch it like it does with C++ references.
If the decision is left until runtime, the only way to deal with it is to specify or compute a default value, which is very much complicated by the special cased 'this' parameter in OO languages.
It's kind of funny that basically everything Java has removed from C++ turns out to be important for a statically typed language. Java should have been a C++ virtual machine with garbage collection.
- masklinn 17y ago> Is he really suggesting to replace all attempts to dereference null with no-ops? Weird. It is. Some languages work that way though, Objective-C for instance: `nil` is a "message sink", any message can be sent to `nil` without generating any error, it simply returns nil.
- fauigerzigerk 17y agoWhat about side effects though? Consider this: obj1 = new SomeClass() obj2 = null obj2.do(obj1.doWithSideEffects()) So obj2.do being a no-op should mean that obj1.doWithSideEffects is never called. Or should it?
- hboon 17y agoIt's called. The do() isn't.
- masklinn 17y agoIt should. [obj1 doWithSideEffects] will be evaluated before [obj2 do] because Obj-C is eager, so the runtime doesn't know (or care) about obj2 when it considers the evaluation of the inner expression. To get the behavior you want, you need to use blocks (which incidentally were introduced in 10.6) and write something along the lines of: [obj2 do: ^{ [obj1 doWithSideEffects] }] In this case, if obj2 do: doesn't explicitly evaluate the block, obj1 doWithSideEffects won't be evaluated.
- hboon 17y agoThe problem with Objective C is, after they have this wonderful message-eating nil, they added this NSNull that brings the problem back. And since it's a superset of C, you get NULL for free too.
- btn 17y agoI don't think you understand what NSNull actually is. It's designed to behave as an empty placeholder object in collections that don't allow nil, nothing more. C's NULL isn't an issue either, nil == NULL == 0, and isn't encountered in any of the contexts described by the article.
- hboon 17y agoSo, given a collection, how do you identify if an element is an instance of NSNull? You do what is essentially a type-check. To me, this negates the purpose of the message-eating nil. Not everywhere, as you seems to be reading, but where NSNull is used, which well, actually, can be anywhere, since we use lots of collections.