[comp.lang.c] The D Programming Language: values and voids

karl@haddock.ISC.COM (Karl Heuer) (04/02/88)

In article <2794@mmintl.UUCP> franka@mmintl.UUCP (Frank Adams) writes:
>The proposal [to make "a=b" a void expression and have "a:=b" take on the
>current assignment semantics] cleans up the semantics by eliminating the
>category of expressions with a non-void type whose value is commonly ignored.
>(Mostly -- there still may be functions returning values which are commonly
>ignored.)

Another case that remains is "++x" (or "x++") as a statement.  And the
compound assignment operators, unless you want to add "+:=" too.

The bit about functions is a problem; if the language were to be strict about
such sloppy return values, several specifications should be changed.  First
off, I'd distinguish an error-exit of a function from a return value (this
would clean up the "int getchar()" botch, too).  Then functions like printf()
should probably be void rather than int.  Now, strcpy() doesn't have an error
return; the reason it has a return value at all is best summarized as "Why
not?".  I would redo it as follows:

  char *_strcpy(char *s, char const *t) {
    while (*t != '\0') *s++ = *t++;
    return s;
  }
  void strcpy(char *s, char const *t) {
    *_strcpy(s, t) = '\0';
  }

Note that the first function returns the end pointer, not the start, and that
it does not store the NUL there.  (If you need access to the end pointer, you
probably have more stuff to write there anyway.)

Karl W. Z. Heuer (ima!haddock!karl or karl@haddock.isc.com), The Walking Lint

decot@hpisod2.HP.COM (Dave Decot) (04/13/88)

> I think this is better written as "if ((status = *statreg))"; some
> people even recommend "if ((status = *statreg), status)".%
> 
> If you write the if as "if (a = b)", many readers of your code
> (possibly including yourself) will first think "aha, this is a typo".
> Many other readers will read it as "if (a == b)".  It is therefore a
> good idea to use a visually distinct form.  The two forms I list above
> both result from the idea that `=' should not be the top level operator
> in a conditional expression.

I agree with the reasoning, but I think an even clearer way of expressing
what you really mean is this:

    if ((status = *statreg) != 0)
       ...

Generally, I will only use an implied comparison to 0 in conditional
contexts when the name of a "Boolean" variable or macro returning
a purely "Boolean" result is the entirety of the condition.

For instance,

    if (strcmp(x, y) != 0)
       ...

but

    if (isprint(*p))
      ...

Dave Decot
hpda!decot