Operator overloading, structs

bearophile bearophileHUGS at lycos.com
Tue Jun 2 13:48:03 PDT 2009


Yigal Chripun:

> why not just use (i != 0) in both cases?

Yes, here it can be done (if Don has implemented opEquals(int y)), but you don't need that once you have something like opBool.


> conversion of ints to bools in C is IMO a hole in the type system due to 
> the lack of a boolean type in C. All those narrowing implicit casts 
> inherited from C are a bad idea IMO.

Walter was right, converting a generic integral to bool requires some time, that in the middle of the the nested loop can slow down code a bit (5%, in a case of mine).
A standard opBool method allows you to tell when a collection is empty, like an empty string, an empty range, an empty array, and so on. you can use it instead of somecollection.isEmpty().
Java as Pascal keep booleans and integers separated, this looks tidy and clean. I love clean things, but in practice I don't think the current design of D (and C, Python, and several other languages) leads to a significant amount of bugs. It seems one of those situations where practical considerations win over purity.
There are other situations where I'd like to see fixed some implicit type conversions. C# shows a saner design in such regard. There are few things in C# that I'd like to see copied by D.


> the need to convert the bigInt to long in order to print it is a design 
> error in printf() - the format string should specify the formatting of 
> the variables not their types.

printf was invented lot of time ago, in galaxy far away, when needs and situations were different.
That code of mine is wrong, this is more correct, so here it's not printf fault:
printf("%d\n", a[to!(long)i]);
Because it's "i" the BigInt, not the contents of the "a" array.

Bye,
bearophile



More information about the Digitalmars-d mailing list