March 04, 2006
Ivan Senji wrote:

> Oskar Linde wrote:
>> && and || controls the program flow.
> 
> If's and while's containing them control program flow (or I didn't get
> something right?)

They control program flow by themselves too:

my_if() && (my_then(),1) || my_else();

Is equivalent to:

if (my_if())
        my_then();
else
        my_else();


>> &,|,~,^ are real operators that also maps directly onto machine instructions on most cpu architectures.
> 
> Sure they are, and I use them all the time on ints.
> 
>> Their meaning would be well defined
>> for bools.
> 
> Not really:
> bool a = //somehow 2
> bool b = //somehow 4
> a && b == true && true == true
> a & b == 0x0010 & 0x0100 == 0x0000 == false,
> 
> I mean they would work but only if true == always 1 and false == always 0.

We all seem to be talking about different kinds of bool :).

[snip]

> In the paragraf you replyed to I was just trying to say that having &,| and ^ for bools only has meaning f they are strict logic bools and not numeric.

I see. I was talking about logical bools. Btw, the current bool (and C99 _Bool, C++ bool) is defined to be either 1 or 0. Without sidestepping the type system there is no way to make a bool take any other value. This means that both numeric operators and bitwise operator are well defined. A bool can never somehow be 2 or somehow be 4. Assigning x to a bool makes an implicit (x!=0). Another numeric boolean representation where the least significant bit defines the truth state would also work with bit operations.

The bool you seem to be talking about is one where any numeric value != 0 would represent true?

/Oskar
March 04, 2006
Ivan Senji wrote:
> xs0 wrote:
>> Even though I don't mind typing something as simple as
>>
>> if (a!=null)
>>
>> the verbosity of longer expressions really annoys the heck out of me. Now, I prefer
>>
>>  > if (a && b && c && d) // check for nulls
> 
> But what is going on behind the scene if a,b,c,d are ints or ponters,
> conversions to bools? I still don't get it how logic && operator can be aplied to integers. What is 5 && 3? (Except two numbers: 5,3.) It has no value regarding truth. It is neither true nor false.
> 
> 

With the behavior implicit conversion from int to bool, "5 && 3" becomes "true && true".



-- 
Bruno Medeiros - CS/E student
"Certain aspects of D are a pathway to many abilities some consider to be... unnatural."
March 04, 2006
Oskar Linde wrote:
> Ivan Senji wrote:
> 
> 
>>Oskar Linde wrote:
>>
>>>&& and || controls the program flow.
>>
>>If's and while's containing them control program flow (or I didn't get
>>something right?)
> 
> 
> They control program flow by themselves too:
> 
> my_if() && (my_then(),1) || my_else(); 
> 
> Is equivalent to:
> 
> if (my_if())
>         my_then();
> else
>         my_else();
> 

Cool, I am never going to use if,then,else again ;)
This is so much cooler :)

> 
>>>&,|,~,^ are real operators that also maps directly onto machine
>>>instructions on most cpu architectures.
>>
>>Sure they are, and I use them all the time on ints.
>>
>>
>>>Their meaning would be well defined
>>>for bools.
>>
>>Not really:
>>bool a = //somehow 2
>>bool b = //somehow 4
>>a && b == true && true == true
>>a & b == 0x0010 & 0x0100 == 0x0000 == false,
>>
>>I mean they would work but only if true == always 1 and false == always 0.
> 
> 
> We all seem to be talking about different kinds of bool :). 

That is true.

> 
> [snip]
> 
> 
>>In the paragraf you replyed to I was just trying to say that having &,|
>>and ^ for bools only has meaning f they are strict logic bools and not
>>numeric.
> 
> 
> I see. I was talking about logical bools. Btw, the current bool (and C99
> _Bool, C++ bool) is defined to be either 1 or 0. Without sidestepping the
> type system there is no way to make a bool take any other value. This means
> that both numeric operators and bitwise operator are well defined. A bool
> can never somehow be 2 or somehow be 4. Assigning x to a bool makes an
> implicit (x!=0). Another numeric boolean representation where the least
> significant bit defines the truth state would also work with bit
> operations.

I did some teseting and it seems to be true. Then my only objection would be if and while taking non boolean arguments, or put differently implicit conversions from bool. But I'm going to stop complaining now and learn to continue to live with how it is now...


> 
> The bool you seem to be talking about is one where any numeric value != 0
> would represent true?
> 

Not really, I'm talking about an i'dont-care-what-it-is bool. :)
March 04, 2006
Bruno Medeiros wrote:
> Ivan Senji wrote:
> 
>> xs0 wrote:
>>
>>> Even though I don't mind typing something as simple as
>>>
>>> if (a!=null)
>>>
>>> the verbosity of longer expressions really annoys the heck out of me. Now, I prefer
>>>
>>>  > if (a && b && c && d) // check for nulls
>>
>>
>> But what is going on behind the scene if a,b,c,d are ints or ponters,
>> conversions to bools? I still don't get it how logic && operator can be aplied to integers. What is 5 && 3? (Except two numbers: 5,3.) It has no value regarding truth. It is neither true nor false.
>>
>>
> 
> With the behavior implicit conversion from int to bool, "5 && 3" becomes "true && true".
> 

I know, but I still see 5 && 3 in my code! :)
March 05, 2006
(your KNode is breaking threads!)

Oskar Linde wrote:
> Also, not allowing ^ for bools would mean that code that reads a^b today would have to be rewritten as:
> 
> (a && !b) || (!a && b).

Err... what you want is just: a != b
March 05, 2006
Miles wrote:

> (your KNode is breaking threads!)

I apologize. I didn't notice that KNode put a single group followup-to-header instead of keeping cross-posting (I assume this is what you are referring to). I will remove the follow-up from this post, but I think KNodes behavior is correct. It is stupid to keep cross-posting this discussion on the two probably most read D newsgroups.

> Oskar Linde wrote:
>> Also, not allowing ^ for bools would mean that code that reads a^b today would have to be rewritten as:
>> 
>> (a && !b) || (!a && b).
> 
> Err... what you want is just: a != b

That's right. I somehow overlooked that. :)

/Oskar
March 05, 2006
Oskar Linde wrote:
> I apologize. I didn't notice that KNode put a single group followup-to-header instead of keeping cross-posting (I assume this is what you are referring to). I will remove the follow-up from this post, but I think KNodes behavior is correct. It is stupid to keep cross-posting this discussion on the two probably most read D newsgroups.

It is a bug, indeed. See https://bugs.kde.org/show_bug.cgi?id=68732 .

This "misfeature" causes much more confusion than solution, and it forces something that is not natural. KNode sets a followup-to header to groups that you are not subscribed. Most of time, it also sets stupid followups (like now, this discussion is much more relevant to D thant to D.announce, and your KNode set it to D.announce). To finish, if people replying to your message don't notice, they end up posting to newsgroups they are perhaps not even subscribed, and will never see their own posts. Even if they notice, it is a pain to manually fix the destination.

This behavior of KNode is more annoying than anything, and it makes it looks like cross-posts are something evil, while in fact they are useful and there is a perfect reason for them.

Well... this is getting way off-topic...
March 06, 2006
Ivan Senji wrote:
> xs0 wrote:

>> I used to think so too, until I was forced to code in Java :) 
> 
> I also coded in Java and never found this to be a problem :)

Sure, it has an easy "workaround", but like I said, I don't think the added verbosity helps, rather the opposite.


>> Even though I don't mind typing something as simple as
>>
>> if (a!=null)
>>
>> the verbosity of longer expressions really annoys the heck out of me. Now, I prefer
>>
>>  > if (a && b && c && d) // check for nulls
> 
> But what is going on behind the scene if a,b,c,d are ints or ponters,
> conversions to bools? I still don't get it how logic && operator can be aplied to integers. What is 5 && 3? (Except two numbers: 5,3.) It has no value regarding truth. It is neither true nor false.

Well, I believe that is just a matter of education/documentation. As much as you need to know that && will short-circuit, you also need to know that it will evaluate the operands as booleans. And, while 3 or 5 are definitely neither true nor false in themselves, one can certainly define a conversion between them and booleans..

It's somewhat like Java's evaluation of "abc"+123. It's implied that 123 will be converted to a string form, and no-one seems to mind, even though there is no implicit relationship between numbers and strings (no, there isn't; 123 as a string could just as easily be "321"(*) or "123.0000000000000", "onetwothree" or "WARNING: auto-conversion of 123 of type int to java.lang.String").. Would you also prefer

"abc"+String.valueOf(123)

or even

// hey, who ever heard of a sum of strings?
StringBuffer sb=new StringBuffer();
sb.append("abc");
sb.append(123);
return sb.toString();

?

I mean, compared to almost everything else in programming, is it really so hard to grasp/learn/remember/know/whatever that 0 and null evaluate to false, while everything else evaluates to true?


xs0

*) this could be called a little-endian string :)
March 06, 2006
xs0 wrote:
> Well, I believe that is just a matter of education/documentation. As much as you need to know that && will short-circuit, you also need to know that it will evaluate the operands as booleans. And, while 3 or 5 are definitely neither true nor false in themselves, one can certainly define a conversion between them and booleans..

Sure, only question: does it makes sense to do that? Judging by the mayority of opinions it does make sense.

> 
> It's somewhat like Java's evaluation of "abc"+123. It's implied that 123 will be converted to a string form, and no-one seems to mind, even though there is no implicit relationship between numbers and strings (no, there isn't; 123 as a string could just as easily be "321"(*) or "123.0000000000000", "onetwothree" or "WARNING: auto-conversion of 123 of type int to java.lang.String").. Would you also prefer
> 
> "abc"+String.valueOf(123)

it could be like in C#(if I remember correctly): "abc" + 123.ToString();
Well i certainly don't miss this aspect of Java in D. There is a good way (maybe a little longer) to do that in D.

> 
> or even
> 
> // hey, who ever heard of a sum of strings?
> StringBuffer sb=new StringBuffer();
> sb.append("abc");
> sb.append(123);
> return sb.toString();
> 
> ?

No I wouldn't prefer these.
I prefer format("abc",123); :)

> 
> I mean, compared to almost everything else in programming, is it really so hard to grasp/learn/remember/know/whatever that 0 and null evaluate to false, while everything else evaluates to true?

No, the problem is and never was in neither grasping nor learning nor remembering this behavior. The problem was that some people see booleans as mathematicians, and in math bools can only be true or false, and can only be a result of operations like <,==,>=, !is, ...

> 
> 
> xs0
> 
> *) this could be called a little-endian string :)

Cool!
March 07, 2006
Walter Bright wrote:
> Lots of new stuff, I added new threads for them in the digitalmars.D newsgroup.
> 
> http://www.digitalmars.com/d/changelog.html

FWIW, here's a(nother) practical example why bools-that-are-not-ints would be useful.  I hit this bug in my own, real-world code, *TODAY*.

I am writing a small compiler.  In it, I build parse trees and expression trees.  Some of these types had a field 'is_array'.  Today I decided that it would be better to store the array length instead, so I replaced 'is_array' with 'array_length in each struct.  I then had to modify all of the code that used those fields.

In a dozens of places I had tests like:
	if(expr->value.is_array)
which I needed to convert to
	if(expr->value.array_length > 0)

Yet, in a number of cases, I ended up doing, instead:
	if(expr->value.array_length);

Likewise, at least once I turned a test like
	if(!expr->value.is_array)
into
	if(!expr->value.array_length > 0)

Which are very hard to find...I'm just gradually finding these problems as I run my test suite on the program.  They would have been compile errors if bools and ints didn't have implicit casts between them.