Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2017-04

Constants in generics

7 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

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

Reply by email

Maciej Izak
to Ryan Joseph

2017-04-09 5:18 GMT+02:00 Ryan Joseph :

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

Reply by email

Ryan Joseph
to Maciej Izak
On Apr 9, 2017, at 6:13 PM, Maciej Izak 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

Reply by email

Sven Barth
to Ryan Joseph
"Ryan Joseph" wrote 7 lines
> On Apr 9, 2017, at 6:13 PM, Maciej Izak 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

Reply by email

Unknown author
to Ryan Joseph: Thanks, that’s a seriously funky hack but it works. :) The only downside is you need to…
Ryan Joseph wrote 12 lines
> On Apr 9, 2017, at 6:13 PM, Maciej Izak 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 :-)

Reply by email

Sven Barth
to Unknown author
Show quoted text 18 lines
On 2017-04-09 06:32, Ryan Joseph wrote: >> >> On Apr 9, 2017, at 6:13 PM, Maciej Izak 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

Reply by email

Ryan Joseph
to Unknown author: The only way I can think of extending the functionality of a array is to put it into an…
On Apr 12, 2017, at 10:06 PM, 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

Reply by email