🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.modula2
Article #2961

Re: Variable types

#2961
From: "Mark Morgan Lloyd "
Date: Fri, 27 Oct 1995 13:42
59 lines
1982 bytes
> Hi,
> 
> In Modula-2, is it possible to call a procedure which
> has paramenters of AnyType ? e.g
> 
> PROCEDURE AddOne(VAR number:AnyType);
> (* code here *)
> END AddOne;

There are two ways, but neither is perfect:

PROCEDURE AddOne(VAR number: ARRAY OF BYTE);

In this case you may be able to deduce the type of operation required by 
looking at the size of the parameter, i.e. CASE HIGH(number). 
Alternatively, move the parameter to a tagged variant record and process 
that:

CONST   numberType= (smallCardinal, smallInteger, cardinal...

TYPE    number= RECORD
                  CASE type: numberType OF
                    smallCardinal: scValue: SHORTCARD |
                    smallInteger:  siValue: SHORTINT |
                    cardinal:      cValue: CARDINAL...
                    
VAR     value: number;
                    

PROCEDURE addOne(VAR num: number);

(* Increment the parameter, assuming that the tag value is set  *)
(* up correctly so that the type is known.                      *)

BEGIN
  WITH num DO
    CASE num.type OF
    ...
      smallInteger: INC(num.siValue) |
      ...
END addOne;      


BEGIN
  ...
  value.type:= smallInteger;
  value.smallInteger:= thisIsASmallInteger;
  addOne(value);
  

Neither of these is particularly good, but I would tend to use one or the 
other rather than spending another fortnight re-reading the gentle 
discussion on operator overloading that has been raging hereabouts :-)

Mark Morgan Lloyd
mark_tbu@cix.compulink.co.uk

[Opinions above are the author's, not those of his employers or 
colleagues]
Technical Details
Message-ID: <DH40rK.6CJ@cix.compulink.co.uk>
Path: arc1-ca.pugleaf.net!cix.compulink.co.uk!usenet
References: <46q6dp$dcv@macondo.dmu.ac.uk>

Show Full Headers
Complete Article Headers
Newsgroups: comp.lang.modula2
Path: arc1-ca.pugleaf.net!cix.compulink.co.uk!usenet
From: mark_tbu@cix.compulink.co.uk ("Mark Morgan Lloyd ")
Subject: Re: Variable types
Message-ID: <DH40rK.6CJ@cix.compulink.co.uk>
Organization: KDG Mobrey Telemetry
References: <46q6dp$dcv@macondo.dmu.ac.uk>
Date: Fri, 27 Oct 1995 13:42:55 GMT
X-News-Software: Ameol