Wasted Efforts (Parametric polymorphism?)



Will "templates" or "generics" or some other thing solve the problem I describe below? Or is this a new invention for pascal?

And even if below solves problems that are solved in generics or templates, does that necessarily mean that we can't come up with a better solution.. i.e. avoiding hacks!

Below solution doesn't cause exe slow down nor does below solution actually in fact actually look that bad at all.. but once you do other hacks like $DEFINES it starts to obfuscate the source.

type
astrarray = array of string;
intarray = array of integer;

var
arrs: astrarray;
arri: intarray;

procedure add(const val: string; var arr: intarray);
var len: integer;
begin
len:= length(arr);
setlength(arr, len+1);
arr[len]:= val;
end;

// this is the same!! just integer is different
procedure add(const val: integer; var arr: intarray);
var len: integer;
begin
len:= length(arr);
setlength(arr, len+1);
arr[len]:= val;
end;

// i.e. this is the goal
procedure add(const val: $thetype; var arr: array of $thetype);
var len: integer;
begin
len:= length(arr);
setlength(arr, len+1);
arr[len]:= val;
end;

Somehow.. $thetype must be replaced with ANY TYPE!
Can't do this in current pascal that I know of..
Sure you can write/use a preprocessor.. but... come on, come on!

Possible solution that is a cool temporary work around:
---INCLUDE FILE---
var len: integer;
len:= length(arr);
setlength(arr, len+1);
arr[len]:= item;
---END INCLUDE FILE---


procedure add(const item: string; var arr: astrarray);
{$INCLUDE cool.inc}
end;

procedure add(const item: integer var arr: intarray);
{$INCLUDE cool.inc}
end;

procedure add(const item: AnyType var arr: AnyTypeArray);
{$INCLUDE cool.inc}
end;

procedure add(const item: integer var arr: RecordArray);
{$INCLUDE cool.inc}
end;

You get the idea... cool.inc is reused!

This is a form of "Parametric polymorphism" that I am reinventing..?

But what will solve it?

And, why can't I use this right now as a workaround? In fact I am considering it for some array stuff I use.. I often add things to arrays. I know most of you use lists and Tlists and all that but I'm a spartan old school programmer looking for good solutions that save me from using a TList and etc.

My solution above uses no overhead or language slowdown...as I've heard generics do.

You can declare any procedure to do the same thing basically.. no matter what "type" of Array you are using.

There may be a way to improve my above solution:
1. compiler support (but in what way? slow down the exe?)
2. using more tricks.. like defines.. nah, rather have compiler support
3. macros.. preprocessor.. again.. no thanks for a long term solution


--
L505
.



Relevant Pages

  • Re: why doesnt ruby have generics?
    ... That would be a type restricted Array but not a generic Array. ... You cannot have generics in a language whose variables are typeless as Arlen pointed out. ... But strictly speaking there is a difference: Java Generics basically are a mechanism for automated casting. ... C++ templates are a completely different story - although they "look" pretty similar to Java's Generics. ...
    (comp.lang.ruby)
  • Re: Announcing CodeGear RAD Studio 2007
    ... genuine quest to determine where generics can be useful outside of the ... oft quoted example of type safe containers. ... if generics may be useful. ... How about an array copier? ...
    (borland.public.delphi.non-technical)
  • Re: A question related to type casting
    ... Given that the distinction already had to be worked around (you couldn't put an int in a List before Java 5), the added complexity would end up being rather pointless. ... Lack of support for value types means more than just the execution overhead of the boxing. ... For example, ArrayList<T> stores the elements in an array of type T. But, there's no way for the class to use an array of ints; it has to be Integer, with each array element a reference to a boxed int, and of course for there to be a separately allocated Integer instance for each non-null array element. ... You're correct that it would have been more complex to support generics in a reifiable way. ...
    (comp.lang.java.programmer)
  • Re: Generic procedures and their parameters
    ... Integer belong to Ordered'Class. ... It's a shaky ground. ... array types based on exactly same combination of index and element types. ... coincidence that "other languages" drift towards supporting generics, ...
    (comp.lang.ada)
  • Pointer to a generic type parameter
    ... I'm new in VC++ and have a question to generics. ... which contains an array of the generic type. ... I wanted to creat a class member which represents the pointer to the ...
    (microsoft.public.dotnet.languages.vc)