DMD 0.149 release
Derek Parnell
derek at psych.ward
Wed Mar 8 03:26:10 PST 2006
On Wed, 08 Mar 2006 21:49:22 +1100, Georg Wrede <georg.wrede at nospam.org>
wrote:
> For C functions that return int to signify truth value, one now (as of
> 0.149) has to do a cast. That becomes cumbersome if one has to do a lot
> of C library usage. It also makes the code less readable.
I'd like to bring up two points about this. Firstly, the strcmp() function
is not really the one you need to have as an example of
C-programs-that-return-an-int-as-a-boolean. This function is actually
designed to return one of *three* values - not a boolean. It returns zero
if the two strings are equal, an integer less than zero if the first
string is less than the second, and an integer greater than zero if the
first string is greater than the second. So the proper way to use this
return value is to compare it to zero.
if (strcmp("foo", datafld ) == 0) // equal strings
if (strcmp("foo", datafld ) < 0) // "foo" is less than datafld
if (strcmp("foo", datafld ) > 0) // "foo" is greater than datafld
if (strcmp("foo", datafld ) != 0) // "foo" is different to datafld
etc ...
Using the result as if it is a boolean is a mistake.
The second point is that C is not D. When interfacing with any foreign
data source, one must expect to do data conversions when the two systems
implement similar paradigms differently. Thus if one is interfacing to a C
function and that C function returns a boolean that is implemented as an
int, the D program ought to convert the C data into the equivalent D data
form. This is a good programming practice.
By the way, if strcmp() was even converted to a pure D function, it still
wouldn't return a bool.
> Seems there are two options
>
> bool a,b,c,d;
>
> a = cast(bool) strcmp("foo", "bar");
> b = cast(bool) strcmp("foo", "baf");
> c = cast(bool) strcmp("foo", "bag");
> d = cast(bool) strcmp("foo", "bad");
>
> or the newly found "cast to bool operator", brought up by BCS (in
> D.announce 3032)
>
> a = !! strcmp("foo", "bar");
> b = !! strcmp("foo", "baf");
> c = !! strcmp("foo", "bag");
> d = !! strcmp("foo", "bad");
>
> This is certainly less typing, and it shrouds less the original code. If
> it weren't for this discovery, I'd have a *hard time* living with this
> last change in D.
But it is wrong to treat strcmp this way, but I understand that this is
not your point really. Clarity in code is to write in such a way as to
make your intentions obvious to other readers of your code.
> So the "!!" might become a much used idiom in D now.
I don't think so...it is too obtuse and not very clear.
>
> ---
>
> (Aside:) In real code of course, one should never invert booleans, and
> one should always use variable names with semantic content for the
> reader. Thus, one probably would write something like
>
>
> bool match;
>
> match = ! strcmp("foo", "bar");
>
> if (match)
> {
> /* do stuff */
> }
> else
> {
> /* call the cops! */
> }
Actually, it probably should be more like ...
match = (strcmp("foo", "bar") == 0);
--
Derek Parnell
Melbourne, Australia
More information about the Digitalmars-d-announce
mailing list