Wasted Efforts (Parametric polymorphism?)
- From: L <L@xxxxxxxx>
- Date: Wed, 19 Mar 2008 07:52:38 -0700
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
.
- Follow-Ups:
- Re: Wasted Efforts (Parametric polymorphism?)
- From: L
- Re: Wasted Efforts (Parametric polymorphism?)
- From: L
- Re: Wasted Efforts (Parametric polymorphism?)
- From: Hans-Peter Diettrich
- Re: Wasted Efforts (Parametric polymorphism?)
- From: Rudy Velthuis [TeamB]
- Re: Wasted Efforts (Parametric polymorphism?)
- From: L
- Re: Wasted Efforts (Parametric polymorphism?)
- From: Craig Stuntz [TeamB]
- Re: Wasted Efforts (Parametric polymorphism?)
- From: Jens Mühlenhoff
- Re: Wasted Efforts (Parametric polymorphism?)
- Prev by Date: Re: Programming Tools Dream
- Next by Date: Re: Wasted Efforts (Parametric polymorphism?)
- Previous by thread: D7 and TE
- Next by thread: Re: Wasted Efforts (Parametric polymorphism?)
- Index(es):
Relevant Pages
|