* * *

Author Topic: Generic Functions, Class Procedure Types?  (Read 1154 times)

darksky666

  • New member
  • *
  • Posts: 30
Generic Functions, Class Procedure Types?
« on: March 06, 2018, 10:25:31 am »
I have read the doc, but couldn't understand these:

1. Is it possible to make generic function in a unit (outside of any class/object)? If yes, how?

2. Why do we need to specialize generic classes? Why can't we create instance without it like in Delphi? (I am reading Marco Cantu book, that's where I found Delphi does not need it, not comparing Laz with Delphi)

3. (IDK if it makes sense but) Can we not create class procedure type:

Code: Pascal  [Select]
  1. type cpt = class procedure (...) of object;

4. I also use Xubuntu, it is not up to date with latest version of lazarus and fpc, it tried to download 3.0.2 the last time I checked. Do you have some PPA I can add to directly update lazarus/fpc instead of manually downloading DEB files?

5. Isn't FPC 3.0.4 DEB and FPC-SRC.DEB (or atleast FPC DEB package) supposed to remain the same if new version of compiler hasn't been released? The size of DEB files of both FPC and FPC-SRC were different between 1.8.0 Laz and 1.8.2.

6. Is it logical/clever practice to mix oop modes (objfpc and Delphi)?

Thanks
« Last Edit: March 06, 2018, 10:38:10 am by darksky666 »

Leledumbo

  • Hero Member
  • *****
  • Posts: 7904
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Generic Functions, Class Procedure Types?
« Reply #1 on: March 06, 2018, 11:16:44 am »
1. Is it possible to make generic function in a unit (outside of any class/object)? If yes, how?
Code: Pascal  [Select]
  1. generic function GenFunc<T>(a,b: T): T;
  2. begin
  3.   Result := a + b;
  4. end;
  5.  
  6. begin
  7.   WriteLn(specialize GenFunc<Integer>(1,2));
  8.   WriteLn(specialize GenFunc<String>('a','b'));
  9. end.
  10.  
2. Why do we need to specialize generic classes? Why can't we create instance without it like in Delphi? (I am reading Marco Cantu book, that's where I found Delphi does not need it, not comparing Laz with Delphi)
Use {$mode delphi} if you want Delphi style, basically the objfpc style is easier to handle at compiler side and it comes before Delphi implements it. Originally it also forces you to specialize at declaration, forcing it to be readable but you can specialize it at execution as well these days.
3. (IDK if it makes sense but) Can we not create class procedure type:

Code: Pascal  [Select]
  1. type cpt = class procedure (...) of object;
Not as far as I can test.
6. Is it logical/clever practice to mix oop modes (objfpc and Delphi)?
Logical: yes. Clever: neutral. For your own code, I think being consistent is better. But when you're working with 3rd party code, you may face different style with yours and that's OK.

darksky666

  • New member
  • *
  • Posts: 30
Re: Generic Functions, Class Procedure Types?
« Reply #2 on: March 06, 2018, 11:39:47 am »
Thank you for replying Leledumbo.. I had read your past post on generic function, I created a new Laz Project (mode objfpc), and added a generic procedure in implementation section, and tried to call it from the FormCreate method, but it gave me an error:

unit1.pas(36,11) Fatal: Syntax error, "identifier" expected but "PROCEDURE" found

Also, If I remove the class type declaration above the generic procedure, it gives me: "BEGIN" expected but "GENERIC" found error..

Am I missing something?

Thanks

___________

This is the code (removed the generic class definition):

Code: Pascal  [Select]
  1.  
  2. unit Unit1;
  3.  
  4. {$mode objfpc}{$H+}
  5.  
  6. interface
  7.  
  8. uses
  9.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     procedure FormCreate(Sender: TObject);
  17.   private
  18.  
  19.   public
  20.  
  21.   end;
  22.  
  23. var
  24.   Form1: TForm1;
  25.  
  26. implementation
  27.  
  28. {$R *.lfm}
  29.  
  30. { TForm1 }
  31.  
  32.   generic procedure proc<T>(item : T);
  33.   begin
  34.     // Content is illogical for use of generics, but just trying
  35.     ShowMessage (IntToStr(item));
  36.   end;
  37.  
  38.  
  39. procedure TForm1.FormCreate(Sender: TObject);
  40. begin
  41.   ShowMessage (specialize proc<integer>(10);
  42. end;
  43.  
  44. end.                
« Last Edit: March 06, 2018, 11:46:53 am by darksky666 »

ASerge

  • Hero Member
  • *****
  • Posts: 767
Re: Generic Functions, Class Procedure Types?
« Reply #3 on: March 06, 2018, 12:54:25 pm »
Am I missing something?
1. This form of generics is not available in FPC 3.0.4 (Lazarus 1.8.2). Only in trunc.
2. You define a procedure, and then try to call ShowMessage (procedure).
3. The IntToStr function has several overloads, either explicitly specify a type, or use the the Format function.
Code: Pascal  [Select]
  1. { TForm1 }
  2.  
  3. generic procedure Proc<T>(Item: T);
  4. begin
  5.   ShowMessage(Format('%d', [Item]));
  6. end;
  7.  
  8. procedure TForm1.FormCreate(Sender: TObject);
  9. begin
  10.   specialize Proc<Integer>(10);
  11. end;

darksky666

  • New member
  • *
  • Posts: 30
Re: Generic Functions, Class Procedure Types?
« Reply #4 on: March 06, 2018, 01:07:07 pm »
Oh so it's not available in the latest release.. I guess I should forget this idea for the time being then.. Thanks ASerge..

ASerge

  • Hero Member
  • *****
  • Posts: 767
Re: Generic Functions, Class Procedure Types?
« Reply #5 on: March 06, 2018, 01:50:34 pm »
Oh so it's not available in the latest release.. I guess I should forget this idea for the time being then.. Thanks ASerge..
In release generics are only template for types.
Code: Pascal  [Select]
  1. type
  2.   generic TProcWrapper<T> = class(TObject)
  3.     class procedure Proc(Item: T); static;
  4.   end;
  5.  
  6. class procedure TProcWrapper.Proc(Item: T);
  7. begin
  8.   ShowMessage(Format('%d', [Item]));
  9. end;
  10.  
  11. procedure TForm1.FormCreate(Sender: TObject);
  12. type
  13.   TIntWrapper = specialize TProcWrapper<Integer>;
  14. begin
  15.   TIntWrapper.Proc(10);
  16. end;

SymbolicFrank

  • Sr. Member
  • ****
  • Posts: 380
Re: Generic Functions, Class Procedure Types?
« Reply #6 on: March 08, 2018, 09:48:16 am »
I strongly recommend using fpcupdeluxe to install a trunk version of fpc/Lazarus if you want to use (Delphi-style) generics.

Thaddy

  • Hero Member
  • *****
  • Posts: 6130
Re: Generic Functions, Class Procedure Types?
« Reply #7 on: March 08, 2018, 09:57:28 am »
That does not matter. OP is making assumptions:
Code: Pascal  [Select]
  1.   generic procedure proc<T>(item : T);
  2.   begin
  3.     // Content is illogical for use of generics, but just trying
  4.     ShowMessage (IntToStr(item));
  5.   end;
It is not only illogical, it will probably crash when T is not resolved to an ordinal type or a real integer type.
I suggest to at the minimum use IntToStrDef or TryIntToStr.

We need a better example.

Second note:
Quote
Code: Pascal  [Select]
  1. type cpt = class procedure (...) of object;
procedural class types won't work. No VMT...for class methods...Documented...
« Last Edit: March 08, 2018, 10:02:25 am by Thaddy »
I might not give the answer that you want me to.. Peter Green 1969

PascalDragon

  • Full Member
  • ***
  • Posts: 109
  • Compiler Developer
Re: Generic Functions, Class Procedure Types?
« Reply #8 on: March 12, 2018, 02:11:52 pm »
That does not matter. OP is making assumptions:
Code: Pascal  [Select]
  1.   generic procedure proc<T>(item : T);
  2.   begin
  3.     // Content is illogical for use of generics, but just trying
  4.     ShowMessage (IntToStr(item));
  5.   end;
It is not only illogical, it will probably crash when T is not resolved to an ordinal type or a real integer type.
I suggest to at the minimum use IntToStrDef or TryIntToStr.

It won't crash, it will result in a compile time error just as if you had written the function with the incorrect type yourself.

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus