Sign in Join

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

Mailing lists / fpc-pascal / 2014-02

How to add new methods in a existent generic type?

5 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

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

silvioprog

Hello,

I'm trying to add new features in a third generic type. Please see this simulation below:

{ TThirdGeneric }

  generic TThirdGeneric<T> = class
  private
    FFoo: T;
  public
    procedure Post;
    property Foo: T read FFoo;
  end;
  { TMyGeneric }
  generic TMyGeneric<T> = class(TThirdGeneric)
  public
    procedure ConfigureFoo;
  end;
  { TMyClass }
  TMyClass = class(TMyGeneric)
  public
    procedure Post;
  end;

...

{ TMyGeneric }

procedure TMyGeneric.ConfigureFoo;
begin
  ... pre configure Foo ...
end;

{ TMyClass }

procedure TMyClass.Post;
begin
  Write(Foo.AProperty); // using pre configured Foo
end;

Just trying to understand the generics concept correctly. :S

Silvio Clécio My public projects - github.com/silvioprog
Sven Barth
to silvioprog
silvioprog wrote 18 lines
Hello, I'm trying to add new features in a third generic type. Please see this simulation below: { TThirdGeneric } generic TThirdGeneric<T> = class private FFoo: T; public procedure Post; property Foo: T read FFoo; end; { TMyGeneric } generic TMyGeneric<T> = class(TThirdGeneric)

This must be "class(specialize TThirdGeneric<T>)" (the "T" is the same type parameter as specified for the "TMyGeneric" declaration, so if it would be "TMyGeneric<MyType>" it would need to be "class(specialize TThirdGeneric<MyType>)")

Show quoted text 7 lines
public procedure ConfigureFoo; end; { TMyClass } TMyClass = class(TMyGeneric)

A class parent must always be a specialization as generics are no valid types per se. So here the correct declaration would again be "class(specialize TMyGeneric<WhateverTypeYouNeed>)".

public procedure Post; end;

[snip]

Just trying to understand the generics concept correctly. :S

Regards,
Sven

silvioprog
to Sven Barth

2014-02-26 13:28 GMT-03:00 Sven Barth :

Show quoted text 49 lines
Am 26.02.2014 16:11, schrieb silvioprog: Hello, > > I'm trying to add new features in a third generic type. Please see this > simulation below: > > { TThirdGeneric } > > generic TThirdGeneric<T> = class > private > FFoo: T; > public > procedure Post; > property Foo: T read FFoo; > end; > > { TMyGeneric } > > generic TMyGeneric<T> = class(TThirdGeneric) > This must be "class(specialize TThirdGeneric<T>)" (the "T" is the same type parameter as specified for the "TMyGeneric" declaration, so if it would be "TMyGeneric<MyType>" it would need to be "class(specialize TThirdGeneric<MyType>)") public > procedure ConfigureFoo; > end; > > { TMyClass } > > TMyClass = class(TMyGeneric) > A class parent must always be a specialization as generics are no valid types per se. So here the correct declaration would again be "class(specialize TMyGeneric<WhateverTypeYouNeed>)". > public > procedure Post; > end; > [snip] Just trying to understand the generics concept correctly. :S > Regards, Sven

Compiled like a charm. I'm loving it. Very very nice!

So, I'll implement some methods in a descendant type of this class:

https://github.com/silvioprog/brookframework/blob/master/core/brookaction.pas#L262

then:

  TjTableEditAction = (eaList, eaCreate, eaUpdate, eaDelete);
  { TjTableEntityAction }

generic TjTableEntityAction<T> = class(specialize TBrookEntityAction<T>)

  public
    { TODO: implement jTable paging -

http://www.jtable.org/apireference#genopt-paging } { TODO: implement jTable edit action -
http://www.jtable.org/apireference#Actions }

  end;

And generics will be very helpful in this task.

Thank you again Sven! (y)

ps. is there any complete documentation about generics in Free Pascal? The Lazarus wiki is a bit incomplete.

Silvio Clécio My public projects - github.com/silvioprog
silvioprog
to silvioprog

2014-02-26 16:15 GMT-03:00 silvioprog :

2014-02-26 13:28 GMT-03:00 Sven Barth :

[...]

Generics power:

https://raw.github.com/silvioprog/dopf/master/demos/entity/demo1.lpr

Soon, I'll:

storage.Entity.Id := 3;
storage.Entity.Name := 'Sven';
storage.Save; // storing entity ...
storage.Modify; // modifying
storage.Move(NewEntity); // moving entity ...
...
for Person in storage.List.Entities do
  WriteLn('ID: ', Person.Id, ' Name: ', Person.Name);

ie. without SQL, but also allowing user to use a specific SQL (link SQL with joins).

Thanks Joao and Sven!

Silvio Clécio My public projects - github.com/silvioprog