From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.1 required=5.0 tests=BAYES_00, PP_MIME_FAKE_ASCII_TEXT autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII X-Google-Thread: 1014db,304c86061dc69dba X-Google-Attributes: gid1014db,public X-Google-Thread: 103376,5cb36983754f64da X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2004-02-11 13:20:24 PST Path: archiver1.google.com!news2.google.com!news.maxwell.syr.edu!pln-e!spln!dex!extra.newsguy.com!newsp.newsguy.com!not-for-mail From: Chris Torek Newsgroups: comp.lang.ada,comp.lang.c Subject: Re: No call for Ada (was Re: Announcing new scripting/prototyping language) Date: 11 Feb 2004 21:03:36 GMT Organization: None of the Above Message-ID: References: <20040206174017.7E84F4C4114@lovelace.ada-france.org> <54759e7e.0402081525.50c7adae@posting.google.com> <1a61f7e5.0402111131.2667fa6@posting.google.com> NNTP-Posting-Host: p-754.newsdawg.com X-Newsreader: trn 4.0-test76 (Apr 2, 2001) Originator: torek@elf.torek.net (Chris Torek) Xref: archiver1.google.com comp.lang.ada:5456 comp.lang.c:21953 Date: 2004-02-11T21:03:36+00:00 List-Id: (I restricted this to just the two groups, although comp.lang.c only might be even more appropriate. The article quoted first below is not available to me so I have only the quotes themselves.) >Ludovic Brenta wrote in message >news:... >> which is not possible in C: >> >> typedef enum { YES, NO, MAYBE, MAYBE_NOT } assessment_t; >> >> void P (assessment_t A) { } >> >> int main () { >> P (42); // no compiler check! >> return 0; >> } In article Rob Thorpe writes: >In C enums are interchangable with ints so there is no error, though >there maybe the compiler should give a warning. More precisely, while "enum" "{" "}" defines a new enumerated type, the members of the id-list are simply ordinary integral constants, and the enumerated type is "compatible with" an integral type (which one is not specified, and different enums might choose different integral types, so that a "small" enum is stored in a char while a "big" one uses an int). As such, enum types are not as useful as one might hope. Note that the "typedef" keyword is a red herring: as with so many of C's keywords, it means the opposite of what it claims. :-) In this case it means "do not define a new type named assessment_t; define assessment_t as an alias for the type this identifier would have if this were not a typedef". For instance, "typedef int A, *B;" makes A a synonym for "int" and B a synonym for "int *". C's most useful type-definer is actually the "struct" keyword, which is perhaps best thought of as standing for "STRange spelling for User-defined abstraCt Type". (Or you can just read it as a weird way to spell the word "type". All new types are thus record types.) The sequence: struct assessment_t { int value; }; defines a new type named "struct assessment_t", after which: void P(struct assessment_t A) { } int main(void) { P(42); /* draws a diagnostic */ return 0; } To get constants of type "struct assessment_t" requires C99: #define AS_T_YES ((const struct assessment_t) { 42 }) #define AS_T_NO ((const struct assessment_t) { 99 }) #define AS_T_MAYBE ((const struct assessment_t) { 1 }) #define AS_T_MAYBE_NOT ((const struct assessment_t) { -1 }) Of course, these macros live in a largely-uncontrolled name space (their scope starts after the #define and ends only at the end of the translation unit or a corresponding #undef). Note that the "obvious" method of defining constants: const struct assessment_t AS_T_YES = { 42 }; does not define a constant at all, because in C (but not C++), the "const" keyword, in keeping with tradition :-) , means "do not define a constant; instead, make this ordinary variable read-only". On the other hand, one can use pointers to application-wide read-only variables as a way to fake constants, but then P() needs to take a "struct assessment_t *". C99's aggregate constructors are probably better, where available. (The "const" in the aggregate constructor constants above tells the C compiler that it is allowed to use a single, read-only object to store the resulting aggregate, if its address is taken. If the address is never taken the "const" is not really required, but it could generate slightly better code on some compilers. The need to take aggregates' addresses arises because of C's peculiar treatment of arrays.) (Ada folks reading this might get the impression that I dislike C. This is not the case -- I just think it is "so ugly it's cute", as it were, the way a former girlfriend of mine thought of various animals. But there is no denying that C is full of sharp edges and odd mannerisms, and that it is easier to get good compile-time testing with Ada.) -- In-Real-Life: Chris Torek, Wind River Systems Salt Lake City, UT, USA (40�39.22'N, 111�50.29'W) +1 801 277 2603 email: forget about it http://web.torek.net/torek/index.html Reading email is like searching for food in the garbage, thanks to spammers.