Re: !foo vs foo == nil
Re: !foo vs foo == nil
- Subject: Re: !foo vs foo == nil
- From: Negm-Awad Amin <email@hidden>
- Date: Fri, 22 Aug 2008 08:02:45 +0200
Am Do,21.08.2008 um 19:54 schrieb Michael Ash:
On Thu, Aug 21, 2008 at 10:40 AM, Negm-Awad Amin <email@hidden
> wrote:
Am Do,21.08.2008 um 16:15 schrieb Michael Ash:
On Thu, Aug 21, 2008 at 6:47 AM, Negm-Awad Amin <email@hidden
>
wrote:
Am Do,21.08.2008 um 12:25 schrieb Thomas Engelmeier:
Am 21.08.2008 um 11:04 schrieb Negm-Awad Amin:
Sometimes you see in source code something like this:
if( booleanVarOrExpression == YES )
(I think, Rentzsch does it that way, IIRC) and Ithink, that
this is
simply correct.
Oh, that is another discussion.
Yeah, but connected
It is antoher discussion in that way, that we have other types.
(BOOL vs.
type of logical expression in C)
It is the same discussion in that way, that most programmers make
some
assumptions about the value (and the casting).
(I bet, that 10 % believe, that the expression inside the if is
typed
BOOL)
I'd be very surprised at this, given that BOOL is found only in
Objective-C, and if statements are found in C.
IIRC this depends on the concrete standard?
No, it does not. Objective-C does not have a standard, and no C
standard includes BOOL.
Objective-C has a reference. The reference includes BOOL.
BOOL
A Boolean value, either YES or NO.
Doing this is absolutly correct.
BOOL var = …; // Something evaluating to YES or NO
if( var == YES )
It's "correct" in that it is a legal language construct and it has
well-defined results. But it is incorrect as far as doing what most
people would want it to do. Consider this:
BOOL var = 2;
If somebody wants to do this, he does something wrong. 2 is no valid
assignment for a BOOL.
Of course it's valid:
typedef signed char BOOL;
This is an implementation detil. You should see the difference between
reference and implementation. This is basical.
Beside this, you misunderstood the meaning of types. If somebody
defines a type, he gives the new type a semantical meaning. Otherwise
there is no need to define a new type.
When $somebody started to define BOOL, bool, Boolean, boolean … he
wanted to say, that this a boolean value. Valid boolean values are YES
ans NO. (or TRUE and FALSE or True and False or true and false - but
boolean values).
The C standard guarantees that a signed char can hold at least values
in the range of -127 to 127. Depending on the implementation, it may
be abel to hold more. Thus any number in that range is a valid value
for a variable of type BOOL.
There is a conception about types. It is not rolling the dices.
if(var == YES)
That if statement will evaluate to false, even though the value of
"var" is conceptually true.
There is no "conpetionally true". BOOL has the valid values YES and
NO, not
2, not CONCEPTIONALLYYES.
No, BOOL has the valid values -127 through 127, and possibly more. In
the C language, all non-zero values are "true".
No, it has the valid vlues YES and NO. This is defined by the language
reference.
("True" in C meaning anything that is not
zero.)
This is not a BOOL. *You* said, that there is no bool in C.
No, I said that there is no BOOL in C.
Of course, when discussion about different languages, it is strange to
refer to a series of lettrs. Shall I start to write BOOL, Bool bool,
Boolean, boolean? There is an idea of booleans. This idea is not
related to programming languages or a specific programming language.
And it is not the idea of boolean numbers, that there is a pari 0, !0.
It is the idea, that there is a pair YES and NO (or TRUE and FALSE or
true and false … The idea does not depend on a specific language)
Thinking about the concept "type" you sould realize, that types are
not made for a special series of characters to name it, but a
semantical conception, meaning.
Of course it is also true that
there is no bool in C. And it's true. But C does have the concept of
logical truth and falsehood, it's just not expressed by means of a
boolean type, or boolean values.
Exactly. But in Objective-C it is expressed by a boolean type.
The logical truth in an if-statement bases on the difference betwenn
== 0 and != 0,
The logical truth of a boolean type in Objective-C is based on YES and
NO.
BOOL
A Boolean value, either YES or NO.
This is, what I said.
so
if( boolean )
changes the type. It only works, because NO is defined as 0 and YES is
defined to something else then 0. As I said hours ago, this is
disclused by the reference, so in Objective-C you can use this
assumption without danger. But you *need* that, because logical
expressions and BOOLs are not the same.
An if takes no
BOOL, but the type of an logical expression.
There is no such thing in the C language as "type of an logical
expression".
Typically logical expression is the term for the superset of
relational expressions and expressions with logical operators. They
have in common, that they only "accepts" the values related to truth
and falseness.
This term isn't inveted by me. I. e.:
http://www.macs.hw.ac.uk/~pjbk/pathways/cpp1/node78.html
Logical expressions is a concept related to many programming
languages. So it is a good idea to use this term in a discussion
touching different languages.
Obviously it is your conception of a programming language to look at
its parser. This is a misconception.
So I really can't tell what you're trying to say here,
except that it must be wrong, because no such type exists.
The if statement in C takes any scalar type as its conditional
expression.
For this reason, I recommend never comparing with YES.
You should recommend, to assign never something else then YES or NO
to a
BOOL. *This* is wrong.
Yes, I *also* recommend this.
Ah, nice to read.
The two recommendations are not mutually
exclusive.
In this case they are.
The first recommendation is correct.
The second recommandation is wrong.
You should *never* assign anything besides YES or NO to
your BOOL variables, and you should *never* compare any BOOL variable
with YES.
It is funny to read, that somebody should never compare a var with one
of its valid values.
Eh, may I complete this:
"You should never compare an int with 2"?
If you think that only one of these suggestions is sufficient,
consider that Cocoa could easily return 2 from the -isEqual: method,
for example.
No, it couldn't. This would be a bug.
isEqual:
Returns a Boolean value that indicates whether the receiver and a
given object are equal.
- (BOOL)isEqual:(id)anObject
ParametersanObject
The object to be compared to the receiver.
Return Value
*YES if the receiver and anObject are equal, otherwise NO.*
a simple
BOOL var = …; // Something evaluating to YES or NO
if( var )
is a very good hidden implicit cast from BOOL to logical
expression.
There is no "hidden implicit cast". The if statement simply checks
its
contents for truth or falseness,
No, it checks for == 0 or != 0.
That's what I said. "Truth" in C is defined as != 0. "Falseness" is
defined as == 0.
Yes, and this is what I said. But this is no BOOL, because for a BOOL
truth is defned as YES and falseness is defined as NO. (Esp. for you:
TRUE|NO, true|no, …)
BOOLs contain YES and NO.
And any other value in the range of -127 to 127.
No.
BOOL
A Boolean value, either YES or NO.
BTW: This is an implicit cast, too:
BOOL var = a && b;
because the result of the right expression types to logical
expression,
not
to BOOL.
No, the right side of the expression is typed *int*.
There is no such
C type as "logical expression".
You cannot use it explicitly, but it is a construction of the
language. The
check of if is on == 0 or != 0. This is less then an int (or
whatever you
use) and something else than a BOOL.
No, there is no such construction of the language. The C if statement
simply checks for equality to zero, period, full stop.
You should start to stumüp with you feet.
BOOLs simply contains YES and NO
BOOL
A Boolean value, either YES or NO.
So it is easy to see, that the checking of an if statement is
something different than a BOOL.
There is no
such concept as "logical expression".
above
Cheers,
Amin
Mike
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
Amin Negm-Awad
email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden