(Copy) (in)compatibility between INTEGER and CARDINAL 
Author Message
 (Copy) (in)compatibility between INTEGER and CARDINAL

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"                             <UI0T>

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






Fri, 19 Mar 1993 23:56:00 GMT  
 (Copy) (in)compatibility between INTEGER and CARDINAL

Quote:

>I'm not sure wether the following message got out correctly. If you

>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

INTEGER and CARDINAL are not compatible within expressions. They may be

Quote:
>Because of that, I have given up using CARDINAL altogether.

Good. In PIM 4th ed. so has Wirth. And he's removed it from Oberon as well.

Quote:
>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?

Wirth designed it this way.

Chuck Lins
--
Chuck Lins               | "Exit left to funway."

20525 Mariani Avenue     | AppleLink: LINS
Mail Stop 41-K           |
Cupertino, CA 95014      | "Self-proclaimed Object Oberon Evangelist"
I speak for myself and no one else.



Fri, 19 Mar 1993 15:33:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. (in)compatibility between INTEGER and CARDINAL

2. CARDINAL and INTEGER number in Mod-2

3. INC(CARDINAL,INTEGER) problem

4. Copying byte sequence from integer/real to character array

5. from ORD to CARDINAL to CONVERT

6. Cardinal numbers

7. Cardinal numbers or unsigned int..

8. Typecasring SHORTCARD/CARDINAL/LONGCARD

9. Getting a CARDINAL into a zero-padded ARRAY OF CHAR

10. Parrots and Cardinals and Ruby, oh my!

11. parrots, cardinals, and rubys - oh my!

12. Cardinal project page on Savannah

 

 
Powered by phpBB® Forum Software