Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!utgpu!water!watnot!watmath!clyde!rutgers!mit-eddie!uw-beaver!ubc-vision!ubc-cs!manis From: manis@ubc-cs.UUCP Newsgroups: comp.sys.atari.st Subject: Re: Type coercion Message-ID: <969@ubc-cs.UUCP> Date: Tue, 7-Apr-87 12:58:59 EST Article-I.D.: ubc-cs.969 Posted: Tue Apr 7 12:58:59 1987 Date-Received: Sat, 11-Apr-87 04:55:26 EST References: <2499@dalcs.UUCP> Reply-To: manis@ubc-cs.UUCP (Vincent Manis) Organization: UBC Department of Computer Science Lines: 74 Keywords: C, Modula-2, long, int, conversion In article <2499@dalcs.UUCP> silvert@dalcs.UUCP (Bill Silvert) writes: >Type coercion in this case means putting both formulations in the same >location by using a union: > > union { long datetime; > structure { int date; > int time; > } pair > } trick; > >(apologies if I haven't got the syntax quite right) >or a variant record in Modula-2 > > TYPE trick = RECORD > CASE : BOOLEAN OF > TRUE : datetime : LONGCARD | > FALSE: date, time : CARDINAL > END > END; > >Then once you have obtained the value of datetime you have date and time >ready to work with, and vice versa, and the conversions are automatic. I won't bother to correct your C syntax, Bill; it's certainly close enough. What you're referring to is not coercion (which just refers to the normal conversions in a language) but "type-cheating". Is it moral? Well, in the strictest sense, no. However, it is impossible to write code which is simultaneously totally portable and yet manages to access system-dependent information. >Pascal also has the variant record feature, but this use of type >coercion is illegal -- whether the compilers on the ST permit it I don't >know. I know of no Pascal compiler which actually checks for consistent use of variants. In particular, Pascal allows untagged variants (just like C unions), in which of course the programmer can select any variant without regard to which variant is presently valid. Of course, the ISO standard says this is illegal, but all they mean is that the onus is on the programmer (or possibly the compiler) to determine what is actually meant (i.e., how the record is laid out in memory). >Clearly this usage assumes that structures are stored >consecutively, which I think is always the case. You (and anyone who compiles your code) must know how the compiler lays out the information (in that regard, C is safer than Modula-2, because the draft ANSI standard says that fields must be laid out in order of the declarations, while Modula-2 compilers are not so constrained. You also have to beware of alignment bytes which some compilers insert. As a suggestion, here are some guidelines: a) Don't type-cheat more than you have to. b) In C, Pascal, and Modula-2, do type-cheating by means of either an explicit type declaration (as above), or by means of a cast (Pascal doesn't have casts, but Modula-2 and C do). c) Localise type-cheating code to a specific module. d) Put in plentiful comments showing the intent of the code. ----- Vincent Manis {seismo,uw-beaver}!ubc-vision!ubc-cs!manis Dept. of Computer Science manis@cs.ubc.cdn Univ. of British Columbia manis%ubc.csnet@csnet-relay.arpa Vancouver, B.C. V6T 1W5 manis@ubc.csnet (604) 228-6770 or 228-3061 "BASIC is the Computer Science equivalent of 'Scientific Creationism'."