Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: c99 and the lack of warnings when int operations are applied to a bool |
| Date | 2012-04-28 13:26 -0700 |
| Organization | None to speak of |
| Message-ID | <ln62cjwqkl.fsf@nuthaus.mib.org> (permalink) |
| References | (7 earlier) <jnd0fk$8vk$1@dont-email.me> <jndsiu$60v$1@dont-email.me> <4f9ad6b4$0$21924$426a74cc@news.free.fr> <lnehr9vxjt.fsf@nuthaus.mib.org> <4F9B997F.6060901@loria.fr> |
Jens Gustedt <jens.gustedt@loria.fr> writes:
> Am 04/27/2012 08:28 PM, schrieb Keith Thompson:
>> - bool is treated as a scalar but non-numeric type
>> - Each enumerated type is treated as a scalar but non-numeric type
>> (bool could be a special-case enumerated type, with false and true as
>> enumerated constants)
>>
>> - No implicit conversions between numeric and non-numeric types (but
>> casts work as in C)
>
> I don't see any good reason to forbid conversions *to* bool.
Remember that the whole point of this hypothetical language is that it's
reasonably similar to C, but with no concern for compatibility, and
removing features that *I personally* dislike. It's not so much a
proposed replacement for C as it is what C might have been like if I had
designed it myself, knowing what I know now.
Which makes this of questionable topicality, but perhaps it can provide
some insight into the design of C as it actually is.
By not having implicit conversions to bool, you don't have to define the
semantics of such conversions. I find the convention that zero is false
and anything else is true to be arbitrary and unnecessary. (I learned
Pascal before I learned C, and I spent a number of years programming in
Ada. Neither of them has such a convention, and both, IMHO, demonstrate
that a language can get along just fine without it.)
bool (or Boolean, or ...) is a type with values false and true.
Comparisons yield bool results. Conditions must be of type bool. It
requires a little extra typing now and then, but I personally find such
a design cleaner than C's handling of conditions (with type _Bool tacked
on years after the language was originally designed).
> While I can image the confusion that was at the origin of this thread
> of using bool in arithmetic, the converse rules how to interpret
> numeric and pointer types in a boolean context are nice and clean.
Ok, but I find "if (num != 0)" and "if (ptr != NULL)" much clearer than
"if (num)" and "if (ptr)", respectively. For one thing, the more
verbose versions make it more obvious what kind of value is being
tested, if it has a name that's not as obvious as "num" or "ptr".
> Also I don't think that the current conversion rules for integer types
> are ideal, since they are inconsistent between the preprocessor phase
> and the rest of the compilation. Why not just have all integer
> arithmetic happen in intmax_t or uintmax_t (and let the optimizer do a
> bit of work if it can be proven that smaller types can be proven to be
> sufficient) ?
Interesting idea, but you could get some performance glitches as small
changes in an expression alter the optimizer's ability to use a smaller
type. Imagine a small tweak to an expression affecting the speed of an
inner loop by a factor of 10 or so. intmax_t and uintmax_t can be very
inefficient, and in an expression as simple as "x + y" it can be
impossible for the optimizer to prove that it won't overflow.
>> - 0 is not a null pointer constant; instead, there's a keyword (say,
>> "nil") that is the language's *only* null pointer constant
>
> I don't see the value of this either. The problem in C is not with 0
> (and other values) as a null pointer constant but with the
> underspecification of NULL.
The problem is that I dislike the use of an int constant 0 to denote a
null pointer value. It contributes to the confusion about the
relationship between integers and pointers. The comp.lang.c FAQ,
<http://c-faq.com/>, has an entire section on null pointers; in my
hypothetical language, that section could probably be reduced to one
question, if that. (And section 9, "Boolean Expressions and Variables",
would probably also be a lot smaller.)
> Having a catch-all initializer for all data types is quite convenient,
> I would miss that.
I agree; being able to write:
some_type obj = { 0 };
and have it work for *any* type is very nice. I can think of ways to
get similar semantics in my hypothetical language, but this is getting
too long already.
>> - Relational and equality operators yield bool results
>>
>> - Contexts that require conditions (if, for, while, do-while, !, &&, ||,
>> ?:, and whatever else I've forgotten) require a bool expression
>>
>> Some implications:
>>
>> - The NULL macro is unnecessary, and is replaced by the nil keyword.
>
> this would be a good one, why not call it nullptr to keep it in sync
> with C++ ? And in current C an identifier that is forced to the value
> (void*)0 would perfectly play that role.
My hypothetical language explicitly rejects compatibility with C; why
should I care about staying in sync with C++? 8-)}
I think C++ used the name "nullptr" to avoid conflicting with
identifiers used in existing code. I like the name "nil" (and there's
precedent for it or for "null", in other languages).
>> - You'd have to write "if (num != 0)" rather than "if (num)".
>>
>> - You'd have to write "if (ptr != nil)" rather than "if (ptr)".
>
> I personally wouldn't like that one
Fair enough. I do.
>> - There are undoubtedly more implications that would have to be worked
>> out.
>
> I'd add another thing to that list: unprototyped functions. They are
> particularly bad because of the lack of pointer conversion for their
> arguments.
>
> One could enforce an implicit conversion rule to void* for them, with
> the consequence that passing an `int const*` to an unspecific
> parameter wouldn't compile at all, or just forbid unprototyped
> functions completely. I would prefer the later, I think, with the
> consequence of also banning va_arg functions.
I hadn't thought about that particular feature, but yes, old-style
function declarations and definitions would definitely be on the
chopping block. They've been "obsolescent" in C since the 1989
ANSI standard.
I wouldn't ban variadic functions without having a good replacement
for them. printf is error-prone, but very powerful. (C++'s use of
an overloaded "<<" operator is nice, but the fact that specifying
a numeric output format changes the state of the stream is *ugly*.
(But I digress.))
And for those who dislike my hypothetical language, I don't think
there's much chance that I'll ever get around to specifying it, let
alone implementing it.
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
c99 and the lack of warnings when int operations are applied to a bool mathog <dmathog@gmail.com> - 2012-03-23 08:40 -0700
Re: c99 and the lack of warnings when int operations are applied to a bool James Kuyper <jameskuyper@verizon.net> - 2012-03-23 12:07 -0400
Re: c99 and the lack of warnings when int operations are applied to a bool jacob navia <jacob@spamsink.net> - 2012-03-23 21:45 +0100
Re: c99 and the lack of warnings when int operations are applied to a bool Robert Wessel <robertwessel2@yahoo.com> - 2012-03-23 17:17 -0500
Re: c99 and the lack of warnings when int operations are applied to a bool jacob navia <jacob@spamsink.net> - 2012-03-23 23:29 +0100
Re: c99 and the lack of warnings when int operations are applied to a bool Ian Collins <ian-news@hotmail.com> - 2012-03-24 11:48 +1300
Re: c99 and the lack of warnings when int operations are applied to a bool jacob navia <jacob@spamsink.net> - 2012-03-24 23:47 +0100
Re: c99 and the lack of warnings when int operations are applied to a bool Willem <willem@toad.stack.nl> - 2012-03-25 09:05 +0000
Re: c99 and the lack of warnings when int operations are applied to a bool "BartC" <bc@freeuk.com> - 2012-03-25 11:30 +0100
Re: c99 and the lack of warnings when int operations are applied to a bool Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-03-25 21:05 +0100
Re: c99 and the lack of warnings when int operations are applied to a bool Lowell Gilbert <lgusenet@be-well.ilk.org> - 2012-03-25 16:15 -0400
Re: c99 and the lack of warnings when int operations are applied to a bool Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-03-25 22:14 +0100
Re: c99 and the lack of warnings when int operations are applied to a bool James Kuyper <jameskuyper@verizon.net> - 2012-03-25 23:46 -0400
Re: c99 and the lack of warnings when int operations are applied to a bool Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-03-26 12:05 +0100
Re: c99 and the lack of warnings when int operations are applied to a bool Lowell Gilbert <lgusenet@be-well.ilk.org> - 2012-03-26 15:22 -0400
Re: c99 and the lack of warnings when int operations are applied to a bool Gareth Owen <gwowen@gmail.com> - 2012-03-25 11:44 +0100
Re: c99 and the lack of warnings when int operations are applied to a bool Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-03-25 13:06 +0100
Re: c99 and the lack of warnings when int operations are applied to a bool Tim Rentsch <txr@alumni.caltech.edu> - 2012-03-27 11:12 -0700
Re: c99 and the lack of warnings when int operations are applied to a bool Robert Wessel <robertwessel2@yahoo.com> - 2012-03-24 01:15 -0500
Re: c99 and the lack of warnings when int operations are applied to a bool jacob navia <jacob@spamsink.net> - 2012-03-24 23:29 +0100
Re: c99 and the lack of warnings when int operations are applied to a bool mathog <dmathog@gmail.com> - 2012-03-27 09:53 -0700
Re: c99 and the lack of warnings when int operations are applied to a bool Keith Thompson <kst-u@mib.org> - 2012-03-27 11:07 -0700
Re: c99 and the lack of warnings when int operations are applied to a bool James Kuyper <jameskuyper@verizon.net> - 2012-03-27 14:16 -0400
Re: c99 and the lack of warnings when int operations are applied to a bool Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-04-26 07:57 -0400
Re: c99 and the lack of warnings when int operations are applied to a bool jacob navia <jacob@spamsink.net> - 2012-04-26 14:08 +0200
Re: c99 and the lack of warnings when int operations are applied to a bool James Kuyper <jameskuyper@verizon.net> - 2012-04-26 09:09 -0400
Re: c99 and the lack of warnings when int operations are applied to a bool Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-04-26 21:29 -0400
Re: c99 and the lack of warnings when int operations are applied to a bool James Kuyper <jameskuyper@verizon.net> - 2012-04-26 21:58 -0400
Re: c99 and the lack of warnings when int operations are applied to a bool Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-04-26 22:32 -0400
Re: c99 and the lack of warnings when int operations are applied to a bool James Kuyper <jameskuyper@verizon.net> - 2012-04-27 06:31 -0400
Re: c99 and the lack of warnings when int operations are applied to a bool Lanarcam <lanarcam1@yahoo.fr> - 2012-04-27 19:26 +0200
Re: c99 and the lack of warnings when int operations are applied to a bool Keith Thompson <kst-u@mib.org> - 2012-04-27 11:28 -0700
Re: c99 and the lack of warnings when int operations are applied to a bool Lanarcam <lanarcam1@yahoo.fr> - 2012-04-27 20:51 +0200
Re: c99 and the lack of warnings when int operations are applied to a bool ImpalerCore <jadill33@gmail.com> - 2012-04-27 12:17 -0700
Re: c99 and the lack of warnings when int operations are applied to a bool Jens Gustedt <jens.gustedt@loria.fr> - 2012-04-28 09:17 +0200
Re: c99 and the lack of warnings when int operations are applied to a bool Keith Thompson <kst-u@mib.org> - 2012-04-28 13:26 -0700
Re: c99 and the lack of warnings when int operations are applied to a bool Willem <willem@toad.stack.nl> - 2012-04-27 18:11 +0000
Re: c99 and the lack of warnings when int operations are applied to a bool ImpalerCore <jadill33@gmail.com> - 2012-04-27 12:05 -0700
Re: c99 and the lack of warnings when int operations are applied to a bool James Kuyper <jameskuyper@verizon.net> - 2012-04-27 15:32 -0400
Re: c99 and the lack of warnings when int operations are applied to a bool Willem <willem@toad.stack.nl> - 2012-04-27 20:16 +0000
Re: c99 and the lack of warnings when int operations are applied to a bool James Kuyper <jameskuyper@verizon.net> - 2012-04-27 16:45 -0400
Re: c99 and the lack of warnings when int operations are applied to a bool Rui Maciel <rui.maciel@gmail.com> - 2012-04-27 21:26 +0100
Re: c99 and the lack of warnings when int operations are applied to a bool Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-04-27 21:18 -0400
Re: c99 and the lack of warnings when int operations are applied to a bool Rui Maciel <rui.maciel@gmail.com> - 2012-04-27 21:33 +0100
Re: c99 and the lack of warnings when int operations are applied to a bool Keith Thompson <kst-u@mib.org> - 2012-04-27 15:36 -0700
Re: c99 and the lack of warnings when int operations are applied to a bool ImpalerCore <jadill33@gmail.com> - 2012-04-27 08:31 -0700
csiph-web