Re: !foo vs foo == nil
Re: !foo vs foo == nil
- Subject: Re: !foo vs foo == nil
- From: "Michael Ash" <email@hidden>
- Date: Thu, 21 Aug 2008 13:54:25 -0400
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.
>>> 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;
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.
>> 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".
>> ("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 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.
> 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". 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. The two recommendations are not mutually
exclusive. You should *never* assign anything besides YES or NO to
your BOOL variables, and you should *never* compare any BOOL variable
with YES.
If you think that only one of these suggestions is sufficient,
consider that Cocoa could easily return 2 from the -isEqual: method,
for example.
>>> 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.
> BOOLs contain YES and NO.
And any other value in the range of -127 to 127.
>>> 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. There is no
such concept as "logical expression".
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