Board index » delphi » Polymorphism in Pascal

Polymorphism in Pascal

Quote
On Sun, 07 Mar 1999 18:45:09 +0100, Shill <msig...@micronet.fr> wrote:
> Can anyone offer some advice with this problem ?

In Pascal a procedure parameter has to have a type or it has to be a
typeless VAR parameter.

If you are using a typeless VAR parameter and nothing else, your
procedure has no way of knowing which type of parameter has been
actually passed. There is definitely no way of recognizing from inside
a procedure which type of parameter has been passed.

In any case the caller has to give some sort of explicit information
about the size and/or type of the actual parameter - together with the
parameter itself. Thus the polymorphism is necessarily explicit.

One alternative would be

type

  multi=record
   case index:byte of
    0:(b:byte);
    1:(i:integer);
    2:(w:word);
    3:(l:longint);
    4:(s:single);
    5:(d:double)
  end;

Your procedure could look like

        procedure x(m:multi);
        begin
          with m do begin
            case index of
              0: dothis; { get b }
              1: dothat; { get i }
              ...
              5: ...
            end
          end
        end;

The caller would have to give explicitly the correct type information:

        var m:multi;
        ...
        m.index:=5;
        m.d:=pi;
        x(m);
        ...

Regards
Horst

 

Re:Polymorphism in Pascal


Quote
Shill wrote:

> I'd like to write Function G(N : T) : String ;
> Where T is either Byte, Shortint, Word, Integer, Longint, Single,
> Real, Double, Extended or Comp.

sigh!

the same question was filling up the NG two weeks ago.

Pascal (as a language) has type declarations, which are valid
at compile time. There is nothing to handle the polymorphism.
In my opinion it is contradicting the Pascal style of data
handling. If you do not want to use Pascal, why do you use
it?

Most Pascal compilers have some provisions for particular
built in procedures and functions, like Succ and Pred and Ord,
and the Turbo Pascal - invented Inc and Dec are additional
examples. But it is the compiler which handles that by creating
the proper code. The application programmer cannot.

Though there it is a rumor that some of the modern compilers
have provisions like overdrive or whatever name it has, look
for the 32-bit compilers FPC, GNU, TMT, P32 (I do not remember
which has it).
--
Franz Glaser, Glasau 3, A-4191 Vorderweissenbach Austria ++43-7219-7035
Muehlviertler Elektronik Glaser. Industrial control and instrumentation
-----------------------------------------------------------------------
http://members.eunet.at/meg-glaser           mailto:meg-gla...@eunet.at
http://www.geocities.com/~franzglaser  http://members.xoom.com/f_glaser

Re:Polymorphism in Pascal


Delphi 4 supports a C++ style of function overloading. Use this.
Quote
Ing. Franz Glaser wrote in message <36E2E023.3D6CE...@eunet.at>...
>Shill wrote:

>> I'd like to write Function G(N : T) : String ;
>> Where T is either Byte, Shortint, Word, Integer, Longint, Single,
>> Real, Double, Extended or Comp.

>sigh!

>the same question was filling up the NG two weeks ago.

>Pascal (as a language) has type declarations, which are valid
>at compile time. There is nothing to handle the polymorphism.
>In my opinion it is contradicting the Pascal style of data
>handling. If you do not want to use Pascal, why do you use
>it?

>Most Pascal compilers have some provisions for particular
>built in procedures and functions, like Succ and Pred and Ord,
>and the Turbo Pascal - invented Inc and Dec are additional
>examples. But it is the compiler which handles that by creating
>the proper code. The application programmer cannot.

>Though there it is a rumor that some of the modern compilers
>have provisions like overdrive or whatever name it has, look
>for the 32-bit compilers FPC, GNU, TMT, P32 (I do not remember
>which has it).
>--
>Franz Glaser, Glasau 3, A-4191 Vorderweissenbach Austria ++43-7219-7035
>Muehlviertler Elektronik Glaser. Industrial control and instrumentation
>-----------------------------------------------------------------------
>http://members.eunet.at/meg-glaser           mailto:meg-gla...@eunet.at
>http://www.geocities.com/~franzglaser  http://members.xoom.com/f_glaser

Re:Polymorphism in Pascal


Quote
Shill wrote...

...
Quote
>I'd like to write Function G(N : T) : String ;
>Where T is either Byte, Shortint, Word, Integer, Longint, Single,
>Real, Double, Extended or Comp.

...

May be you can implement the folloaing instead.
Function G(N : string) : string;  

OR

mytype = (byte, shortint, word, integer, lonint, ...);
Function G(N : string; what_type : mytype) : string;

If Turbo Pascal doesn't offer you polymorphism, you could modify
your design instead.

Re:Polymorphism in Pascal


Quote
Shill wrote:

> Here I go.

> Function F(N : Word) : String ;
> F returns the hexadecimal representation of N.
> i.e.
> F(65535) returns 'FFFF'
> F(43981) returns 'ABCD'

> I managed to write F from the excellent FAQ by Timo Salmi.
> But I want to go a step further: I want N to be polymorphic.

> I'd like to write Function G(N : T) : String ;
> Where T is either Byte, Shortint, Word, Integer, Longint, Single,
> Real, Double, Extended or Comp.

This is generally called "generic"; "polymorphic" is what
you get with objects and virtual functions.

TP/BP (and Standard Pascal) does not have generic functions.
(Or, to be more precise, it doesn't give you a way to define
them; write and inc are obviously generic.)

[...]

Quote
> I learned a bit of C and Caml. In C, I think I'd use a Union Type and
> in Caml I'd use an Enumeration but I scanned the different types
> available in Pascal and didn't find anything obvious.

If you are happy with what you could do with an union type
in C, it's simple:

type
  VarType = (vtChar, vtShortint, vtByte, vtInt, vtWord, vtLongint,
             vtReal, vtSingle, vDouble, vtExtended, vtReal);

function G(var v, vtype: VarType): string;
 var
  size : word;
 begin
  case VarType of
   vtChar:       size:=sizeof(char);
   vtShortint:   size:=sizeof(shortint);
   ...
  end;
  ... convert size bytes of v ...
 end;

And call that with

var
 i: integer;
 r: real;

begin
 writeln(G(i, vtInt));
 writeln(G(r, vtReal))
end.

However, I suspect that you could then just give the size directly.

What you really seem to want is the equivalent to C++'s templates.

Quote

> Can anyone offer some advice with this problem ?

> Shill The (Wannabee) Mad Coder

In BP I don't see a chance.
I don't know if there's any Pascal compiler which implements
template-like features.

IMHO generic functions would make a nice addition to Pascal.

A simple syntax could look like this:

procedure swap(var x: type T; var y: T);
 var
  tmp: T;
 begin
  tmp:=x;
  x:=y;
  y:=tmp
 end;

Here the explicit "type" would tell the compiler that this
is a generic parameter: it's a definition of the type T as
type of the parameter x. From there on, the type could be
used like any other type in its scope.

Re:Polymorphism in Pascal


Ing. Franz Glaser <meg-gla...@eunet.at> wrote:

Quote
> [...] look
> for the 32-bit compilers FPC, GNU, TMT, P32 (I do not remember
> which has it).
> --

definately fpc. but plz forget it as fast as you can :)

greetings, tilman.

Other Threads