Class/Record helpers on genericd?

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Class/Record helpers on genericd?

Anthony Walter-3
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.

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

Re: Class/Record helpers on genericd?

Sven Barth-2

Am 25.04.2016 22:44 schrieb "Anthony Walter" <[hidden email]>:
>
> 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.

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

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
|

Re: Class/Record helpers on genericd?

Maciej Izak
2016-04-26 7:57 GMT+02:00 Sven Barth <[hidden email]>:

> 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

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

Re: Class/Record helpers on genericd?

Anthony Walter-3
In reply to this post by Sven Barth-2
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);

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