Path: utzoo!utgpu!attcan!uunet!wuarchive!gem.mps.ohio-state.edu!pacific.mps.ohio-state.edu!tut.cis.ohio-state.edu!ucbvax!DKAUNI2.BITNET!UI0T
From: UI0T@DKAUNI2.BITNET (Thomas Koenig)
Newsgroups: comp.lang.modula2
Subject: (in)compatibility between INTEGER and CARDINAL
Message-ID: <"89-09-30-14:36:30.85*UI0T"@DKAUNI2.BITNET>
Date: 30 Sep 89 21:02:37 GMT
Sender: daemon@ucbvax.BERKELEY.EDU
Reply-To: Modula2 List 
Organization: The Internet
Lines: 36
X-Unparsable-Date: Sat, 30 Sep 89 14:36:00 LCL

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 ...

produce 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