Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!ucbvax!DKAUNI2.BITNET!UI0T From: UI0T@DKAUNI2.BITNET (Thomas Koenig) Newsgroups: comp.lang.modula2 Subject: (Copy) (in)compatibility between INTEGER and CARDINAL Message-ID: <"89-10-02-15:04:30.36*UI0T"@DKAUNI2.BITNET> Date: 2 Oct 89 23:56:44 GMT Sender: daemon@ucbvax.BERKELEY.EDU Reply-To: Modula2 ListOrganization: The Internet Lines: 45 X-Unparsable-Date: Mon, 2 Oct 89 15:04:00 LCL I'm not sure wether the following message got out correctly. If you have already read it, forget this one. ---------------------------- Text of forwarded message ----------------------- Date: Sat, 30 Sep 89 13:57 From: "Thomas Koenig" To: info-m2@ucf1vm.bitnet Subject: (in)compatibility between INTEGER and CARDINAL On the Modula-2 compiler I use (SPC-Modula2 V1.42 for the Atari ST) there are some problems if I use INTEGER and CARDINAL together in a statement. For example, the input PROCEDURE Something(a:ARRAY OF CHAR; n:INTEGER); VAR i:CARDINAL; BEGIN FOR i:=0 to HIGH(a) DO IF i=n THEN ... produces two incompatibility errors, one each in the last two lines. HIGH returns an INTEGER in this implemenation, and this kind of 'mixing' the two types is forbidden by the compiler. To get the same result, I would have to use auxiliary variables (I can say n:=i if n is an INTEGER and i a CARDINAL) or explicit type conversion with SYSTEM.VAL. I think the first is bad for program readability and the second is dangerous because the use of VAL switches off range checking. In the compiler I use, there is no other type conversion procedure with the help of which I could, for example, write IF CARDINAL(n)=i THEN ... and get appropriate error messages if n<0. Because of that, I have given up using CARDINAL altogether. My question is: is this incompatibility between CARDINAL and INTEGER a kludge in the compiler, was it meant to be that way by Wirth or is this kind of conversion something left to whoever writes a compiler? I just hope the answer is not going to be 'well, this implicit type conversion shouldn't really work, but most compilers do it anyway...' Thomas Koenig UI0T@DKAUNI2.BITNET UI0T%DKAUNI2.BITNET@CUNYVM.CUNY.EDU (soon) UI0T@IBM3090.RZ.UNI-KARLSRUHE.DE