I don't know about Pascal, but in C++, it's easy to accomplish (and usually the goal anyway). Just look at the STL. It is possible in C, but not so pretty, to say the least:
[code]
struct typeinfo
{
void * (*increment)(void*);
void * (*decrement)(void*);
void (*copy)(void*, void*);
int (*compare)(void *, void*);
};
struct typeinfo *
typeinfo_init(
struct typeinfo * info,
void * (*increment)(void*),
void * (*decrement)(void*),
int (*compare)(void *, void*),
void (*copy)(void*, void*)
)
{
info->increment = increment;
info->decrement = decrement;
info->compare = compare;
info->copy = copy;
return info;
}
struct type
{
void * begin;
void * end;
};
struct type *
type_init(struct type * data, void * begin, void * end)
{
data->begin = begin;
data->end = end;
return data;
}
unsigned
copy(struct type * dest, struct type * src, struct typeinfo * specific)
{
void * db = dest->begin, * sb = src->begin;
unsigned result = 0;
while(db != dest->end && sb != src->end)
{
specific->copy(db, sb);
db = specific->increment(db);
sb = specific->increment(sb);
++result;
}
return result;
}
void *
find(struct type * data, void * what, struct typeinfo * specific)
{
void * db = data->begin;
while(db != data->end)
{
if(0 == specific->compare(db, what))
return db;
db = specific->increment(db);
}
return 0;
}
void *
char_inc(void * p)
{
return ++(char*)p;
}
void *
char_dec(void * p)
{
return --(char*)p;
}
int
char_comp(void * a, void * b)
{
return *(char*)a - *(char*)b;
}
void
char_copy(void * a, void * b)
{
*(char*)a = *(char*)b;
}
[code]
And a sample proggie:
Code:
int
main()
{
char src[] = "Success.";
char dest[100] = "Failed.";
char c = 'c';
struct typeinfo char_type;
struct type s, d;
typeinfo_init(&char_type, char_inc, char_dec, char_comp, char_copy);
type_init(&s, &src[0], &src[strlen(src)]);
type_init(&d, &dest[0], &dest[100]);
copy(&d, &s, &char_type);
char * found = find(&d, &c, &char_type);
printf("dest: %s\nfound: %s\n", dest, (found ? found : "(null)"));
return 0;
}
I think that's what make C++ such a powerful language - the fact that the same functionality can be accomplished with so much less effort.