[comp.lang.modula2] Type transfer

kbad@atari.UUCP (Ken Badertscher) (06/06/89)

In article <342@actisb.UUCP> federico@actisb.UUCP (Federico Heinz) writes:
| ... Thanks for the information on type-transfer functions, though
| (as usual, quite, but not totally unlike the standard.  I love it).

I suspect that the PD Modula-2 compiler is based on Wirth's 1-pass compiler.
Type transfer functions (apparently deemed "evil" by the ETH Zurich team
that came up with the 68000 1-pass compiler) were implemented with VAL
in that compiler.  The rationale they gave was that type transfer, as an
inherently unstructured construct, _should_ be difficult to use.

This I learned while studying the 1-pass compiler in my apprenticeship
;-) at Jefferson Software.

I'm inclined to agree, but to avoid fostering Religious Type Wars, I
won't enumerate the reasons why.

-- 
   |||   Ken Badertscher  (ames!atari!kbad)
   |||   Atari R&D System Software Engine
  / | \  #include <disclaimer>

jbaker@gmu90x.gmu.edu (jbaker) (11/16/89)

I have a question regarding type transfer in Modula-2.
Is it legal to perform type transfer using a type such as this:

TYPE
  String = ARRAY[0.255] OF CHAR;

VAR
  str1 : ARRAY[0..255] OF CHAR;
  str2 : String;

...
  str2 := String(str1);
...

This code sometimes causes a run-time error in my code IF
str1 is not word-aligned. (Apparantly a word level copy is performed.)

Someone at the company which wrote the compiler told me that type transfer
of structures is illegal in Modula-2.  I had never heard this before;
in fact, the reference book I use (Modula-2 Wizard by Richard S. Wiener)
states that the "type identifier of any type may be used to transfer the
                                    ^^^
value of any other type of the same size into that type."
         ^^^

Now, the only way that I can see both the book and the compiler to be correct
(but not the guy I called) is if ARRAY[0..255] OF CHAR is not really a type,
but an array.  Thus the "source type" is really non-existant.  Thus my
compiler would then work since types of size > one byte are word-aligned.

Does anyone else have a book which explains this situation better?

   - John Baker

K315640@AEARN.BITNET (Wolfgang Schreiner) (11/17/89)

Reply to John Baker's question about type transfer in Modula-2:

I guess, your trouble about type transfers arises from the fact, that
type transfers represent low-level facilities that are NOT DEFINED in
the Modula-2 language definition. Wirth writes in his "Programming in
Modula-2" (second edition, page 125):

   " ... The correspondences (of transferred types) are NOT defined
     by the language Modula. They must be supplied by additional
     system-dependent information."

Therefore type transfers depend on the actual implementation of the
compiler one uses. So neither the text book you use nor the guy you
asked are right. Type transfer functions are low level facilities
that should be used only in machine-dependent system programs. They
are NOT intended for high-level programming (for this purpose, Modula
provides explicite type conversion on basic types by the standard
procedure VAL).

jbaker@gmu90x.gmu.edu (jbaker) (11/17/89)

In article <INFO-M2%89111611481682@UCF1VM> Modula2 List <INFO-M2%UCF1VM.BITNET@PSUVM.PSU.EDU> writes:
>Reply to John Baker's question about type transfer in Modula-2:
>
>I guess, your trouble about type transfers arises from the fact, that
>type transfers represent low-level facilities that are NOT DEFINED in
>the Modula-2 language definition. Wirth writes in his "Programming in
>Modula-2" (second edition, page 125):
>
>   " ... The correspondences (of transferred types) are NOT defined
>     by the language Modula. They must be supplied by additional
>     system-dependent information."
>
>Therefore type transfers depend on the actual implementation of the
>compiler one uses. So neither the text book you use nor the guy you
>asked are right. Type transfer functions are low level facilities
>that should be used only in machine-dependent system programs. They
>are NOT intended for high-level programming (for this purpose, Modula
>provides explicite type conversion on basic types by the standard
>procedure VAL).

I take Wirth's quote to mean that transferred types will not necessarily
result in the same values on different machines.  i.e. if 1234 (CARDINAL)
corresponds to 1.234 (REAL) on one machine, it may correspond to 4.321
on another.  (Unrealistic of course, but you get the idea.)  However, even
though both of my types were exactly the same length, the type transfer
itsself caused a run-time error!  Extending "correspondence" to include
the alignment of variables is an unfortunate burden on Modula-2 programmers.

I strongly disagree with your statement that type transfers should only
be used in system programs.  Nearly every good Modula-2 program will
use the CARDINAL and INTEGER type transfers at least.  In order to
increment a pointer, it is frequently necessary to convert it to type
ADDRESS and back.  The list goes on and on.  The ability to transfer
types is one of my favorite enhancements over PASCAL.

    - John Baker