(in)compatibility between INTEGER and CARDINAL 
Author Message
 (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 ...

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






Wed, 17 Feb 1993 21:02:00 GMT  
 (in)compatibility between INTEGER and CARDINAL
Can't you say something like

FOR i:= 1 to CARDINAL(HIGH(n)) DO BEGIN

etc..
?

-Mark



Fri, 19 Mar 1993 21:58:00 GMT  
 (in)compatibility between INTEGER and CARDINAL

Quote:

> Can't you say something like

> FOR i:= 1 to CARDINAL(HIGH(n)) DO BEGIN

> etc..
> ?

> -Mark

Unfortunately, this is not possible in SPC-Modula2 - I get an error
which sounds something like 'illegal kind of procedure result'.  So I
guess I'll drop CARDINAL from my Modula 'vocabulary' altogether and
just try to rewrite whatever comes my way from other programmers
(sigh) ...






Fri, 19 Mar 1993 20:45:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. (Copy) (in)compatibility between INTEGER and CARDINAL

2. CARDINAL and INTEGER number in Mod-2

3. INC(CARDINAL,INTEGER) problem

4. from ORD to CARDINAL to CONVERT

5. Cardinal numbers

6. Cardinal numbers or unsigned int..

7. Typecasring SHORTCARD/CARDINAL/LONGCARD

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

9. Parrots and Cardinals and Ruby, oh my!

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

11. Cardinal project page on Savannah

12. Time to resurrect Cardinal (Ruby->Parrot)?

 

 
Powered by phpBB® Forum Software