Re: nil messaging? Is it safe?
Re: nil messaging? Is it safe?
- Subject: Re: nil messaging? Is it safe?
- From: Marcel Weiher <email@hidden>
- Date: Fri, 27 Sep 2002 01:23:59 +0200
On Thursday, September 26, 2002, at 09:39 Uhr, Gerben Wierda wrote:
I did not see the original message, but
You certainly CAN send messages to nil! I do it all the time. It
sends the
message up the responder chain.
That's wrong. The nil 'target' in a target/action specification is not
a message-send to nil. It is a flag in the target/action mechanism to
send to the message up te responder chain. It is not a feature of the
language or compiler (but language/compiler make such featues possible).
"up the responder chain"??
You are right to be skeptical.
According to the Obj-C manual sending a message to nil
"is valid as long as the message returns an object; if it does, a
message sent to nil will return nil. If the message sent to nil
returns anything other than an object, the return value is undefined"
It will the the '0' bit-pattern, whatever that means.
Now, I do not understand this. How can the compiler or the runtime
system know what the message will return?
They don't know what the message *does* return, but they have to know
what *type* of value it is declared to return. In fact, it has to know
this in order to generate the appropriate code at the call-site.
After all, nil has no class, so there is no way of knowing the
signature of the message (that is, the return value).
The compiler decides (at compile time) what signature it will compile
code for.
I was under the impression that any message sent to nil will just
return nil and that's it.
Yup.
No harm done.
Usually.
But both statements above do suggest something else is going on.
The responder-chain part is bogus. Apart from that, the "zero" bit
pattern may not mean something useful.
So: is it safe to send messages to nil?
Yes. It is just that it may or may not be safe to use the return value.
What does the runtime system exactly do with messages like that?
It tosses them.
Marcel
--
Marcel Weiher Metaobject Software Technologies
email@hidden www.metaobject.com
Metaprogramming for the Graphic Arts. HOM, IDEAs, MetaAd etc.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.