Recent

Author Topic: Bounded Type Parameters?  (Read 5165 times)

blondMammuth

  • New Member
  • *
  • Posts: 16
Bounded Type Parameters?
« on: September 23, 2014, 02:24:26 pm »
I want to do something I have done often in Java:

I want to define a generic class C with a type parameter <T> such that T implements a given Interface I. Or in order to be more general,  the implementation of C should take use of the fact that instances of  T offer a given function f.

How can I do that? My idea would have been:

type

I = interface
  function f: Boolean;
end;

generic C <T: I> = class
  function g(t:T):Boolean;
end;

implementation

function C.g(t:T);
begin
  g:=t.f;
end;

According to the language reference, this is syntactically not possible. Is there any other way to achieve this kind of genericity? Can I write generic code for a restricted variety of type parameters?

Thanks in advance!

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: Bounded Type Parameters?
« Reply #1 on: September 23, 2014, 08:46:47 pm »
Code: [Select]
type
  TBooleanProc = function(): boolean of object;
  // Most likely that you want "of object" if your boolean method is within a class
 
  C = class
    function g(t: TBooleanProc): boolean;
  end;

  D = class(C)
    function boolTest(): boolean;
    procedure Run();
  end;

implementation

function C.g(t: TBooleanProc): boolean;
begin
  result:=t();
end;

function D.boolTest(): boolean;
begin
  result:=true;
end;

procedure D.Run();
var b: boolean;
begin
  b:=g(@boolTest);
end;

Not sure if it's exactly what you needed but should get it started.

rausm

  • Newbie
  • Posts: 1
Re: Bounded Type Parameters?
« Reply #2 on: September 23, 2014, 09:06:50 pm »
It is also called Constrained Generics.
googling for freepascal generic constraints yielded http://lists.freepascal.org/fpc-announce/2012-December/000586.html  (Feature announcement: Generic type constraints) on the first place.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Bounded Type Parameters?
« Reply #3 on: September 24, 2014, 05:09:48 am »
Generics contraints have been implemented in FPC trunk.

blondMammuth

  • New Member
  • *
  • Posts: 16
Re: Bounded Type Parameters?
« Reply #4 on: September 26, 2014, 11:08:25 pm »
Thanks, great answers!

blondMammuth

  • New Member
  • *
  • Posts: 16
Re: Bounded Type Parameters?
« Reply #5 on: September 26, 2014, 11:42:35 pm »
Especially thanks for the link.

Yet another thought: I read about future ideas for type constraints like "object" or "array" and "enum" and so on, and that they might be introduced in future. My suggestion to that: why not provide special predefined interfaces for them? That would eliminate the need for new Syntax.

This may not be the right place for this kind of suggestions. Is there a better place to discuss these issues?

Mike.Cornflake

  • Hero Member
  • *****
  • Posts: 1260
Re: Bounded Type Parameters?
« Reply #6 on: September 27, 2014, 10:15:46 am »
This may not be the right place for this kind of suggestions. Is there a better place to discuss these issues?

Sounds like you're after the fpc mail lists
http://lists.freepascal.org/mailman/listinfo
Lazarus Trunk/FPC Trunk on Windows [7, 10]
  Have you tried searching this forum or the wiki?:   http://wiki.lazarus.freepascal.org/Alternative_Main_Page
  BOOKS! (Free and otherwise): http://wiki.lazarus.freepascal.org/Pascal_and_Lazarus_Books_and_Magazines

blondMammuth

  • New Member
  • *
  • Posts: 16
Re: Bounded Type Parameters?
« Reply #7 on: September 27, 2014, 12:17:11 pm »
Yes, thanks!

Mike.Cornflake

  • Hero Member
  • *****
  • Posts: 1260
Re: Bounded Type Parameters?
« Reply #8 on: September 27, 2014, 12:22:09 pm »
If you want to browse past archives, you can use
http://lists.freepascal.org/pipermail/fpc-pascal/

March 2025 was an interesting inclusion :-)
Lazarus Trunk/FPC Trunk on Windows [7, 10]
  Have you tried searching this forum or the wiki?:   http://wiki.lazarus.freepascal.org/Alternative_Main_Page
  BOOKS! (Free and otherwise): http://wiki.lazarus.freepascal.org/Pascal_and_Lazarus_Books_and_Magazines

blondMammuth

  • New Member
  • *
  • Posts: 16
Re: Bounded Type Parameters?
« Reply #9 on: September 27, 2014, 12:38:29 pm »
 :D
« Last Edit: September 27, 2014, 12:40:31 pm by blondMammuth »

 

TinyPortal © 2005-2018