Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!utgpu!water!watnot!watmath!dalcs!silvert From: silvert@dalcs.UUCP Newsgroups: comp.sys.atari.st Subject: Type coercion Message-ID: <2499@dalcs.UUCP> Date: Sat, 4-Apr-87 08:48:52 EST Article-I.D.: dalcs.2499 Posted: Sat Apr 4 08:48:52 1987 Date-Received: Sun, 5-Apr-87 08:49:43 EST Organization: Marine Ecology Lab. Lines: 48 Keywords: C, Modula-2, long, int, conversion Since I occasionally post source to the net, I thought it might be good to try to reach a consensus on whether type coercion is desirable in portable code, or whether, as the books say, it is a despicable and dangerous practice which ranks with goto's. I refer specifically to date and time functions, which sometimes use two separate integer/CARDINAL variables (I refer to C/Modula-2 here), but sometimes pack both into a long/LONGCARD variable. Conversion of one to the other involves either arithmetic or bitswitching, and can lead to problems such as the recently reported type-extension bug in settime and autodisk. 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 have used this in my own datesetting program and find it convenient, but wonder if it creates problems with other compliers and languages. 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. Clearly this usage assumes that structures are stored consecutively, which I think is always the case. Anyway, comments on this usage would be appreciated. If I get a lot of mail, I will post the conclusions to the net. -- Bill Silvert, Modelling/Statistics Group Bedford Institute of Oceanography, Dartmouth, NS, Canada CDN or BITNET: silvert@cs.dal.cdn -- UUCP: ..!{seismo|utai}!dalcs!silvert ARPA: silvert%dalcs.uucp@seismo.CSS.GOV -- CSNET: silvert%cs.dal.cdn@ubc.csnet