[fpc-pascal] Constants in generics

Sven Barth pascaldragon at googlemail.com
Tue Nov 6 14:21:57 CET 2018


Am Di., 6. Nov. 2018, 08:44 hat Ryan Joseph <ryan at thealchemistguild.com>
geschrieben:

> I implemented a first draft of constants (integers) in generics. My reason
> was specifically that I wanted a way to add methods to static arrays and
> generic records is the only way to accomplish this AFAIK.
>
> If I fix this up will it be considered as a patch? I wanted to present the
> idea first before I spent any more time. Here’s what I has so far (on
> GitHub).
>
>
> https://github.com/genericptr/freepascal/commit/ec518542b2da7d7f016702a82b2d05349a01a6fb
>
> {$mode objfpc}
> {$modeswitch advancedrecords}
>
> program generic_constants;
>
> type
>         generic TList<T, U> = record
>                 list: array[0..U-1] of T;
>                 function capacity: integer;
>         end;
>
> function TList.capacity: integer;
> begin
>         result := U;
> end;
>
> var
>         nums: specialize TList<integer,10>;
>         strs: specialize TList<integer,4>;
> begin
>         writeln('sizeof:',sizeof(nums), ' capacity:',nums.capacity);
>         writeln('sizeof:',sizeof(strs), ' capacity:',strs.capacity);
> end.
>

First of I'm not a fan of adding support for constants, mainly because it
will definitely not help parsing of inline specializations in mode Delphi
which are going to be annoying enough already.
That said: even if we do add it, then only if a generic constant parameter
is designated as such with "const N" in the generic declaration instead of
merely "N", so that the compiler does not assume it's a type.

Regards,
Sven

>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20181106/b2c449b0/attachment.html>


More information about the fpc-pascal mailing list