Board index » delphi » More on Polymorphism

More on Polymorphism

Quote
Shill wrote:
> The polymorphism geek is back with another question.

> I find the built-in function "random" very polymorphic.

At least in terms of TP, the term you want that more closely describes this is
"overloaded".  "Polymorphic" surfaces in the context of object-oriented
design, where objects adapt in specialization as they go through inheritance.
This should be covered in the User's Guide, and I don't think it's reall
necessary to go futher into that because it's not the main case here.

Quote
> I can call it either with no parameter or one parameter and the return
> type is either real or the type of the parameter, if I understand
> correctly.

Yes and no.  At source level, there's only one function.  In reality, the
compiler analyzes the source and internally remaps the random() reference to
RandInt() for integer, RandReal() for 6-byte reals, or RandFloat() for
floating point types.

Quote
> Could I write such a function ?

No.  The *only* unit that provides this kind of support is System, and this is
because it's specially built and the compiler maintains an internal list of
cues for parsing your source code.

Quote
> Another related question:
> Functions with an indefinite number of parameters like "writeln". How
> would I write one ?

It uses a simlar method, except that it breaks down each comma delimited
expression into a function call designed to handle that particular data type
(string, Boolean, char, integer, real, etc.)  And, no, this method cannot be
duplicated immediately either.  You *can* write a TFDD, or Text File Device
Driver, to handle text streams in a custom manner, but you'll still be
restricted to the same sort of writeln()-style per-type processing.

--
Scott Earnest            | SPAM protection in effect. Remove  |
setech@_ix.netcom.com    | "_" as needed for true addresses.  |
earnests@_homenet.lm.com |    UIN:1136443  EFnet:pale_blue    |
sinykal@_{*word*104}space.org  | URL: http://www.netcom.com/~setech |

 

Re:More on Polymorphism


Quote
Shill wrote:
> The polymorphism geek is back with another question.
> I find the built-in function "random" very polymorphic.
> I can call it either with no parameter or one parameter and the return
> type is either real or the type of the parameter, if I understand
> correctly.
> Could I write such a function ?

Again... in Pascal not, in Ada (***) yes:

generic
  type rand_value is digits <>;

function Random_gen(max: rand_value:= 1.0) return rand_value;

function Random_gen(max: rand_value:= 1.0) return rand_value is
 ...
end;

Then you can instanciate a "Random" for any real type and do such a
thing with any integer type...
Random         -- real value between 0 and 1
Random(10.0)   -- real value between 0 and 10
Random(10)     -- integer value between 0 and 10

Quote
> Another related question:
> Functions with an indefinite number of parameters like "writeln". How
> would I write one ?

In C++ (***) I believe you can do so (>> ?)...

Writeln and Rand are not real Pascal procedures ;-) !

--
(***) Cultural discussion, no adverti{*word*224}t: I'm a Pascal fan !
      [this is why I like Ada so much... 8-)]

--
Gautier

Re:More on Polymorphism


Quote
Shill wrote:

> Another related question:
> Functions with an indefinite number of parameters like "writeln". How
> would I write one ?

     Not directly.  However, you could write a function/procedure that
would "walk" and process a linked list, thereby allowing it to act as
though you passed it a variable number of parameters (what you are
really doing, of course, is passing it a single parameter, a list, that
has a variable length).  Thus while one cannot write a Pascal function
MAX that returns the max of 2, 3, 4, ... arguments, it is easy to write
a function MAX (list : listptr) that returns the maximum element in a
(non-empty) list of arbitrary length.

Bob Schor
Pascal Enthusiast

Re:More on Polymorphism


In article <36EA3267.D992741F@_ix.netcom.com>, Scott Earnest
<setech@_ix.netcom.com> writes

Quote
>At least in terms of TP, the term you want that more closely describes this is
>"overloaded".

And this is one of the things you can't do (yourself) in TP. Didn't
believe it. Tried it. Believed it. In the TP reference manual, no
mention. In Entsminger [1-55851-155-5]:

  "...operator overloading isn't a requirement in OOP... it makes things
   look more like what the programmer had in mind... perhaps future
   versions of TP will incorporate this feature."

You can do it in C++ though! (oops - wrong newsgroup - 'pologies).

--
Marcus Morris - South Croydon, LONDON, UK (Mar...@ntos.demon.co.uk)

Other Threads