Pretty generic functions
Original Reporter info from Mantis: anonymous coward
-
Reporter name:
Original Reporter info from Mantis: anonymous coward
- Reporter name:
Description:
When I learned about generic classes, it suggested generic functions maybe added later. I don't think the usual way of doing templates suits pascal so, here are my suggestions instead:
The first extension is the "arb" prefix (as in "arbitrary"). For simplicity, I'll refer to types prefixed with arb as "arb types". An arb type can be any type but all instants must be same type.
Here's an example:
procedure swap(var a,b: arb t); {the arb t here must be the same type as...}
var
temp: arb t = a; {...this arb t here.}
begin
a := b;
b := temp
end;
The second extension is the "aggressive inline" modifier (tell me if you can think of a better name :) ). It's like the inline modifier but allows recursion.
Here's an example:
function factorial(x: 2..max_cardinal): cardinal; forward; aggressive inline;
begin
factorial := factorial(n-1) * n
end;
function factorial(x: 0..1): cardinal; inline;
begin
factorial := 1
end;
The last suggestion deals with correctly overloading function defined with arb types (specialization). One way to solve this is just using the order but, I'm not sure if that's the best way.
Here's an example:
procedure swap(var a,b: array[arb range] of arb base_type); forward;
{yes, I know no one would actually use this}
var
i: arb range;
begin
for i := low(arb range) to high(arb range) do swap(a[i], b[i])
end;
procedure swap(var a,b: arb t);
var
temp: arb t = a;
begin
a := b;
b := temp
end;
Mantis conversion info:
- Mantis ID: 12958
- Fixed in version: 3.1.1
- Monitored by: » anonymous coward (anonymous coward)