Re: Untyped variables of a function
Dr Engelbert Buxbaum <[email protected]> Wed, 29 Dec 2021 11:22:08 +0100
| Newsgroups | alt.comp.lang.pascal |
|---|---|
| Organization | private |
| Message-ID | <[email protected]> |
In article <[email protected]>, [email protected] says... > >Am 02.09.2021 um 00:53 schrieb Shadow: > > > >>> Pascal has strong types. Is it anyhow possible to define a > >>> parameter of a function or procedure that allows passing variables > >>> of any type? In Turbo Pascal (and its descendants Delphi and Object Pascal) you can pass untyped variables to functions and procedures. For example: PROCEDURE InitLIFO(VAR L: LIFO); BEGIN L := NIL; END; (* InitLIFO *) FUNCTION EmptyLIFO(L: LIFO): BOOLEAN; BEGIN Result := L = NIL; END; (* EmptyLIFO *) PROCEDURE PUSH(VAR L: LIFO; VAR I; S: WORD); VAR p: LList; BEGIN NEW(p); WITH p^ DO BEGIN Size := S; Next := L; GetMem(Info , Size); Move(I, Info^, Size); END; (* WITH *) L := p; END; (* Push *) PROCEDURE POP(VAR L: LIFO; VAR I; S: WORD); VAR p: LList; BEGIN p := L; WITH p^ DO BEGIN IF Size <> S THEN BEGIN Writeln('TYPE-MISMATCH-ERROR'); HALT; END; Move(Info^, I, Size); FreeMem(Info , Size); L := Next; END; (* WITH *) DISPOSE(p); END; (* Pop *) In other words, PUSH and POP do not need to know the content of I, all they care about is its size (so the call is, e.g., PUSH(L, I, SizeOf (I))). Note that untyped variables are always var-parameters, even if unchanged.