Sign in Join

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

Mailing lists / fpc-pascal / 2016-04

Class/Record helpers on genericd?

4 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

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

Anthony Walter

Sven,

Correct me if I'm wrong, but didn't you add support for class/record helpers on generics? For example I'd like to be able to add methods to dynamic arrays:

type
  TArray<T> = array of <T>;
  TCompare<T> = function(constref A, B: T): Integer;
  TArrayHelper<T> = record helper for TArray<T>
  private
    ...
  public
    procedure Sort(Compare: TCompare);
    function First: T;
    function Last: T;
    function Random: T;
    // and so on
  end;

As it is right now the compiler will not allow this.

Reply by email

Sven Barth
to Anthony Walter
"Anthony Walter" wrote
Sven, Correct me if I'm wrong, but didn't you add support for class/record

helpers on generics? For example I'd like to be able to add methods to dynamic arrays:

Show quoted text 17 lines
type TArray<T> = array of <T>; TCompare<T> = function(constref A, B: T): Integer; TArrayHelper<T> = record helper for TArray<T> private ... public procedure Sort(Compare: TCompare); function First: T; function Last: T; function Random: T; // and so on end; As it is right now the compiler will not allow this.

No, I did not and it's also not planned.

Regards,
Sven

Reply by email

Maciej Izak
to Sven Barth

2016-04-26 7:57 GMT+02:00 Sven Barth :

> As it is right now the compiler will not allow this. No, I did not and it's also not planned.

That is planned in other way, as extra profit of smart pointers (related to new syntax).

Best regards, Maciej Izak

Reply by email

Anthony Walter
to Sven Barth: helpers on generics? For example I'd like to be able to add methods to dynamic arrays:…

Okay. Thanks. Just FYI here is my use case.

{ User defined vertex type }

type
  TColorLitVertex = record
    Position: TVec3;
    Normal: TVec3;
    Color: TVec4;
  end;

{ Contiguous data to be fed to an opengl vertex shader attribute }

var
  VertexData: TArray<TColorLitVertex>;

It would be nice to have both of these types of functionality supported:

VertexData[I].Color.Alpha := 0.5; // you can only write to record fields if
the container is an array

and ..

VertexData.Sort(SortZOrder); // you can only have methods on arrays using
record helpers, but not generic arrays

and later provide a reference to the first color location to opengl (array types make this easiest) ...

glVertexAttribPointer(ColorAttribute, 4, GL_FLOAT, GL_FALSE,
SizeOf(TColorLitVertex), @VertexData[0].Color);

Reply by email