[comp.lang.modula2] CHAR-String compatibility

HPA111@DE0HRZ1A.BITNET ("Michael Drechsler +49 343062", 0201) (04/22/88)

Hello,

Given: PROCEDURE P(S : ARRAY OF CHAR);

If I try to call this proc with a CHAR type argument, the compiler I'm
using (M2/CMS for IBMs running VM/CMS; based on a ETH-compiler(?))
flags a "type incompatibility" error.

I think, that a single char should be compatible because it can be seen
as an ARRAY [0..0] OF CHAR. I have nothing found about that problem in
Wirth's book PIM2 ed.3 (german version).

Does anyone of you knows more about that problem? Perhaps in the english
version of the above book?

But if a CHAR as ARRAY [0..0] OF CHAR is compatible to a formal parameter
ARRAY OF CHAR, then the following should also be valid:

calling a PROCEDURE pp(AnyVar : ARRAY OF AnyType);

with a single variable of type "AnyType", because the single variable
in the procedure call can also be seen as an ARRAY [0..0] OF AnyType.

Is this correct?

Thanks for your help,
Michael

jane@CCA.CCA.COM (Jane Eisenstein) (04/22/88)

The original version of the language (pre edition 3) did differentiate
between a CHAR and an array of char containing a single element. It
looks like you simply have an outdated compiler.

chrisj@cup.portal.COM (04/24/88)

In Message-ID: <INFO-M2%88042209060985@DB0TUI11>, HPA111@DE0HRZ1A.BITNET
("Michael Drechsler +49  343062", 0201) asks about passing a single CHAR
actual parameter to a formal parameter of type ARRAY OF CHAR, or in fact
passing a value of Anytype to a formal parameter of type ARRAY OF Anytype.

Nuthin' doin'.  A quoted literal is a string of some length or other,
even if the length happens to be 1.  That a quoted string of length 1,
although assignable to variable or passable to a parameter of an ARRAY
OF CHAR type, is ALSO compatible with (can be assigned to, and compared
with) type CHAR is a special case for strings only, stated on p. 155 of
3rd edition of PIM2.  (Language report, Section 9.1, Assignments).

When you pass a single character between quote marks to a parameter of
type ARRAY OF CHAR, you are passing a string of length 1, not a character.
It is not at all the same as passing the value of a variable of type CHAR.

Passing a value to a procedure is like assigning the value to a variable
of the same type as the declared type of the parameter, and is allowed
under the same circumstances.

    PROCEDURE P (a : ARRAY OF CHAR);BEGIN END P;
    VAR ch  : CHAR;
        ca  : ARRAY [0..0] OF CHAR;

    ca := ch;       (* WRONG -- TYPES NOT COMPATIBLE *)
    IF ca < ch THEN ... END;  (* WRONG -- TYPES NOT COMPATIBLE *)
    P (ch);         (* WRONG -- TYPES NOT COMPATIBLE *)

    ca [0] := ch;   (* ok *)
    IF ca [0] > ch THEN ... END; (* ok *)

    P (ca);         (* ok *)
    P ("A");        (* String of length 1, same as the previous line. *)

Only in the case of the magic type SYSTEM.WORD (and SYSTEM.BYTE on some
implementations), do you get the special exception that something other
than an array can be passed to a parameter of type ARRAY OF WORD.

Christopher Jewell                |  If God had meant for us to fly, He
chrisj@cup.portal.com             |  would NEVER have given us the railways.
ucbvax!sun!cup.portal.com!chrisj  |                     -- Flanders & Swan