Modula2's godawful IO.

Alan Lovejoy alan at pdn.UUCP
Wed Apr 13 01:29:16 AEST 1988


In article <96 at lzaz.ATT.COM> bds at lzaz.ATT.COM (BRUCE SZABLAK) writes:
>I like C++'s overloading of the << and >> operator's even better than
>printf. It allows you to define custom print routines for each structure
>(class) declared, and then to print (to stdout for example) you do:
>	cout << god_awful_structure_instance;
>The advantage of this approach over using procedures in
>modula2 or pascal is (besides previty) that you don't have to
>remember the type of structure your variable is; the compiler figures it out.

Overloading or procedure-name-abstraction is badly needed in Modula-2.
However, it needs to be applicable to ANY AND ALL procedures and
operators.  For printing, I prefer:

  Print(object, fieldWidth);

The programmer writes a Print procedure for each data type he defines
and/or wants to print.  

Better yet, he writes a "BinToStr" procedure for each data type.  Then Print 
can be defined generically once:    

  PROCEDURE Print(object; fieldWidth: CARDINAL);
    VAR
      str: ARRAY [0..79] OF CHAR;
  BEGIN
    BinToStr(object, fieldWidth);
    Display.WriteString(str);
  END Print;

Notice that this also requires the additional capability to define
parameters of unspecified type.  To compile this procedure, the
compiler generates abstract pseudo-code which is data-type independent 
as its final output instead of object code.  Object code for the procedure 
is generated each time the compiler discovers an actual call to the procedure, 
because only then can it know the data type of the typeless argument and
generate the correct code.
    

-- 
Alan Lovejoy; alan at pdn; 813-530-8241; Paradyne Corporation: Largo, Florida.
Disclaimer: Do not confuse my views with the official views of Paradyne
            Corporation (regardless of how confusing those views may be).
Motto: Never put off to run-time what you can do at compile-time!  



More information about the Comp.lang.c mailing list