Re: nil messaging? Is it safe?
Re: nil messaging? Is it safe?
- Subject: Re: nil messaging? Is it safe?
- From: Ondra Cada <email@hidden>
- Date: Fri, 27 Sep 2002 01:35:00 +0200
On Thursday, September 26, 2002, at 09:39 , Gerben Wierda wrote:
"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"
Now, I do not understand this. How can the compiler or the runtime system
know what the message will return? After all, nil has no class, so there
is no way of knowing the signature of the message (that is, the return
value).
It's a bad description, IMHO.
I was under the impression that any message sent to nil will just return
nil and that's it. No harm done. But both statements above do suggest
something else is going on. So: is it safe to send messages to nil? What
does the runtime system exactly do with messages like that?
The fact is very very simple -- it does "return nil;" the same way as if
the method was declared to return an id. Lowlevel it just means it zeroes
$r3.
As for the "safe" part:
(i) you can *any time* *completely safely* send *any* message to nil, and
it is guaranteed that nothing would crash and nothing happen;
(ii) if you want to use the returned value, the situation changes somewhat:
- in case the returned value is awaited in $r3 (in practice, if it is
an integer or a pointer), it is safe to use it and presume it would be
zero, nil, NULL;
- in case the returned value is awaited elsewhere (in practice, float
returs and struct returns), it is not safe to use it anyhow (the result
would be more or less random).
---
Ondra Cada
OCSoftware: email@hidden
http://www.ocs.cz
private email@hidden
http://www.ocs.cz/oc
_______________________________________________
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.