[fpc-pascal] Feature announcement: implicit generic function specializations
denisgolovan
denisgolovan at yandex.ru
Fri Apr 22 09:23:53 CEST 2022
> If you want to pass a pointer to ^T in a generic function is there anyway safe to do this currently? Pascal doesn’t allow ^ types in function arguments (why?) and generics don’t seems to support pointers either (why?).
>
> generic TValues<T> = array[0..0] of T;
> generic PValues<T> = ^specialize TValues<T>;
>
> I guess the only thing to do is use a untyped pointer and cast it to the correct type inside the function declaration?
>
> For example here is a generic QuickSort function which operates on any array of T.
>
> type
> generic TComparator<T> = function (left: T; right: T; context: pointer): integer;
>
> generic procedure QuickSort<T>(_values: pointer; first, last: LongInt; comparator: specialize TComparator<T>);
> type
> TValues = array[0..0] of T;
> PValues = ^TValues;
> var
> pivot,j,i: integer;
> temp: T;
> values: PValues absolute _values;
>
> Regards,
> Ryan Joseph
>
Not that pretty, but I use something like:
==============================================
generic TPtr<T> = record
public
type P = ^T;
public
ptr: P;
end;
generic function Ref<T>(out r: specialize TPtr<T>.P):boolean;
==============================================
Denis Golovan
More information about the fpc-pascal
mailing list