Constants in generics

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Constants in generics

Ryan Joseph
I wanted to make a record generic that would serve as wrapper for an array allocated on the stack. Is there anyway to make constants work in generics? The generic type compiles but the specialization fails.

type
  generic TFixedList<T, L> = record
                type
                        FData = array[0..L] of T;
  end;

var
        flist: specialize TFixedList<integer, 16>;

Regards,
        Ryan Joseph

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Constants in generics

Maciej Izak

2017-04-09 5:18 GMT+02:00 Ryan Joseph <[hidden email]>:
Is there anyway to make constants work in generics?

sure, but not directly. Try this:

=== code begin ===
{$MODE DELPHI}

type
  TA = record
  public const
    L = 16;
  end;

  TB<T, L> = record
  public type
    FData = array[0..L.L] of T;
  end;

var
  X: TB<Integer, TA>;

=== code end ===

--
Best regards,
Maciej Izak

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Constants in generics

Ryan Joseph

> On Apr 9, 2017, at 6:13 PM, Maciej Izak <[hidden email]> wrote:
>
> sure, but not directly. Try this:

Thanks, that’s a seriously funky hack but it works. :) The only downside is you need to define types every time you need a different size.

Is this a known feature request or anything on the to-do list for the compiler team? There’s no way to extend the functionality of static arrays in Pascal and this would accomplish that well.

Regards,
        Ryan Joseph

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Constants in generics

Free Pascal - General mailing list

Am 09.04.2017 14:03 schrieb "Ryan Joseph" <[hidden email]>:
>
>
> > On Apr 9, 2017, at 6:13 PM, Maciej Izak <[hidden email]> wrote:
> >
> > sure, but not directly. Try this:
>
> Thanks, that’s a seriously funky hack but it works. :) The only downside is you need to define types every time you need a different size.
>
> Is this a known feature request or anything on the to-do list for the compiler team? There’s no way to extend the functionality of static arrays in Pascal and this would accomplish that well.

Constant parameters are definitely not planned for generics.
Generics are already providing enough headaches as they are without adding that as well -.-

Regards,
Sven


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Constants in generics

noreply
In reply to this post by Ryan Joseph
On 2017-04-09 06:32, Ryan Joseph wrote:

>> On Apr 9, 2017, at 6:13 PM, Maciej Izak <[hidden email]> wrote:
>>
>> sure, but not directly. Try this:
>
> Thanks, that’s a seriously funky hack but it works. :) The only
> downside is you need to define types every time you need a different
> size.
>
> Is this a known feature request or anything on the to-do list for the
> compiler team? There’s no way to extend the functionality of static
> arrays in Pascal and this would accomplish that well.
>

The only way I can think of extending the functionality of a array is to
put it into an old borland object (on the stack) but that may not be
what you are looking for. Then you can give the array methods,
effectively, possibly reinventing TStringList ;-) and that array can be
fixed, dynamic, whatever. But as for generics, no, old borland style
objects are not going to be generic - so this email of mine is likely
completely and utterly irrelavent to your problem  domain :-)

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Constants in generics

Free Pascal - General mailing list

Am 12.04.2017 17:06 schrieb <[hidden email]>:
>
> On 2017-04-09 06:32, Ryan Joseph wrote:
>>>
>>> On Apr 9, 2017, at 6:13 PM, Maciej Izak <[hidden email]> wrote:
>>>
>>> sure, but not directly. Try this:
>>
>>
>> Thanks, that’s a seriously funky hack but it works. :) The only
>> downside is you need to define types every time you need a different
>> size.
>>
>> Is this a known feature request or anything on the to-do list for the
>> compiler team? There’s no way to extend the functionality of static
>> arrays in Pascal and this would accomplish that well.
>>
>
> The only way I can think of extending the functionality of a array is to put it into an old borland object (on the stack) but that may not be what you are looking for. Then you can give the array methods, effectively, possibly reinventing TStringList ;-) and that array can be fixed, dynamic, whatever. But as for generics, no, old borland style objects are not going to be generic - so this email of mine is likely completely and utterly irrelavent to your problem  domain :-)
>

Generic TP-style objects are supported in FPC, but you can use advanced records (which allow methods as well as management operators) as well.

Regards,
Sven


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Constants in generics

Ryan Joseph
In reply to this post by noreply

> On Apr 12, 2017, at 10:06 PM, [hidden email] wrote:
>
> The only way I can think of extending the functionality of a array is to put it into an old borland object (on the stack) but that may not be what you are looking for. Then you can give the array methods, effectively, possibly reinventing TStringList ;-) and that array can be fixed, dynamic, whatever. But as for generics, no, old borland style objects are not going to be generic - so this email of mine is likely completely and utterly irrelavent to your problem  domain :-)

My problem is I wanted a way to specify a static array size use generics, specifically so I could add methods to use on the array. It’s basically the same talk we’re having now but with static arrays, i.e. I want to add functions and build an API.

type
        generic TList<T,L> = record
                list: array[0..L-1];
                procedure Add (elem: T);
        end;


Regards,
        Ryan Joseph

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Loading...