Recent

Author Topic: Helper functions for Integers and other primitive types  (Read 663 times)

carl_caulkett

  • Hero Member
  • *****
  • Posts: 594
Helper functions for Integers and other primitive types
« on: October 06, 2024, 12:55:32 am »
* Mac Mini M1
* macOS 14.6.1
* Lazarus 3.99
* FPC 3.3.1

I notice that it is possible to use code such as "Count.ToString" where Count is an Integer. Is this done by something similar to class helpers, and can it be added to, or is it some "behind the scenes" IDE magic taking place?
« Last Edit: October 07, 2024, 03:40:15 pm by carl_caulkett »
"It builds... ship it!"

TRon

  • Hero Member
  • *****
  • Posts: 3453
« Last Edit: October 06, 2024, 01:00:19 am by TRon »
This tagline is powered by AI

jamie

  • Hero Member
  • *****
  • Posts: 6681
Re: Helper functions for Integers and other primitive types
« Reply #2 on: October 06, 2024, 01:00:53 am »
you can add to them, btw.
The only true wisdom is knowing you know nothing

TRon

  • Hero Member
  • *****
  • Posts: 3453
Re: Helper functions for Integers and other primitive types
« Reply #3 on: October 06, 2024, 01:02:57 am »
Indeed you can jamie but be aware that afaik trunk supports mutliple helpers while current release FPC requires subclassing the old typehelper in order to extend.
« Last Edit: October 06, 2024, 01:06:52 am by TRon »
This tagline is powered by AI

VisualLab

  • Hero Member
  • *****
  • Posts: 518
Re: Helper functions for Integers and other primitive types
« Reply #4 on: October 06, 2024, 01:24:32 am »
In particular, type helpers work very well for your own enumeration types. I've been using them for a long time.

Thaddy

  • Hero Member
  • *****
  • Posts: 15929
  • Censorship about opinions does not belong here.
Re: Helper functions for Integers and other primitive types
« Reply #5 on: October 06, 2024, 07:02:14 am »
It is not IDE magic. It is is built into the language. You can use the helpers in any editor
If I smell bad code it usually is bad code and that includes my own code.

carl_caulkett

  • Hero Member
  • *****
  • Posts: 594
Re: Helper functions for Integers and other primitive types
« Reply #6 on: October 07, 2024, 12:56:34 am »
I've just discovered that you can use TStringHelpers on the class itself, rather than just instances of a string... so: "LineStr := string.Create(LineChar, 180);"
This is so cool  :D

I see from the docs at https://www.freepascal.org/docs-html/rtl/sysutils/tstringhelper.html that the members of TStringHelper are split into "class function" and "function" so that explains why my discovery works ;)
« Last Edit: October 07, 2024, 01:01:17 am by carl_caulkett »
"It builds... ship it!"

TRon

  • Hero Member
  • *****
  • Posts: 3453
Re: Helper functions for Integers and other primitive types
« Reply #7 on: October 07, 2024, 01:09:39 am »
@carl_caulkett:
Or things like:
Code: Pascal  [Select][+][-]
  1.   writeln('remove me and keep this'.Remove(0,9));
  2.  
This tagline is powered by AI

carl_caulkett

  • Hero Member
  • *****
  • Posts: 594
Re: Helper functions for Integers and other primitive types
« Reply #8 on: October 07, 2024, 12:59:41 pm »
I've tried to define my own helper for Integers using inheritance, with this...
Code: Pascal  [Select][+][-]
  1.   TcaIntegerHelper = type helper(TIntegerHelper) for Integer
  2.   public
  3.     function Pred: Integer;
  4.   end;
  5.  

and by using multihelpers with this...

Code: Pascal  [Select][+][-]
  1. {$modeswitch multihelpers}
  2.  
  3. uses
  4.   ... Sysutils, ...
  5.  
  6. type
  7.   TcaIntegerHelper = type helper for Integer
  8.   public
  9.     function Pred: Integer;
  10.   end;
  11.  

In both cases it's falling over at the TcaIntegerHelper definition with...
Code: Text  [Select][+][-]
  1. camidimac.pas(17,27) Error: Identifier not found "helper"
  2.  

What am I doing wrong?

« Last Edit: October 07, 2024, 01:02:20 pm by carl_caulkett »
"It builds... ship it!"

Thaddy

  • Hero Member
  • *****
  • Posts: 15929
  • Censorship about opinions does not belong here.
Re: Helper functions for Integers and other primitive types
« Reply #9 on: October 07, 2024, 01:31:31 pm »
Demo inherited helpers:
Code: Pascal  [Select][+][-]
  1. program helpers;
  2. {$if fpc_fullversion < 30301}{$error this program needs trunk}{$endif}
  3. {$mode objfpc}{$H+}
  4. {$modeswitch typehelpers}
  5. uses sysutils;
  6. type
  7.   TMyIntegerHelper = type helper(TintegerHelper) for integer
  8.     procedure inc(const num:integer = 1);
  9.   end;
  10.  
  11.   procedure TMyIntegerHelper.inc(const num:integer);
  12.   begin
  13.     system.inc(self,num);
  14.   end;
  15.  
  16. var
  17.   i:integer = 100;
  18. begin
  19.   i.inc;
  20.   writeln(i);// 101
  21.   i.inc(99);
  22.   writeln(i);// 200
  23. end.
« Last Edit: October 07, 2024, 01:49:35 pm by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

Thaddy

  • Hero Member
  • *****
  • Posts: 15929
  • Censorship about opinions does not belong here.
Re: Helper functions for Integers and other primitive types
« Reply #10 on: October 07, 2024, 01:42:02 pm »
Demo multihelpers (needs, trunk/main 3.3.1)
Code: Pascal  [Select][+][-]
  1. program helpers2;
  2. {$if fpc_fullversion < 30301}{$error this program needs trunk}{$endif}
  3. {$mode objfpc}{$H+}
  4. {$modeswitch typehelpers}
  5. {$modeswitch multihelpers}
  6. uses sysutils;
  7. type
  8.   TIntegerHelper = type helper for integer
  9.   procedure inc(const num:integer = 1);
  10.   end;
  11.  
  12.   procedure TIntegerHelper.inc(const num:integer);
  13.   begin
  14.     system.inc(self,num);
  15.   end;
  16.  
  17. var
  18.   i:integer = 100;
  19. begin
  20.   i.inc;
  21.   writeln(i);
  22.   i.inc(99);
  23.   writeln(i.tostring);
  24. end.
Both examples need trunk/main, otherwise they won't compile
« Last Edit: October 07, 2024, 01:51:16 pm by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

Thaddy

  • Hero Member
  • *****
  • Posts: 15929
  • Censorship about opinions does not belong here.
Re: Helper functions for Integers and other primitive types
« Reply #11 on: October 07, 2024, 01:54:40 pm »
Demo for version < fpc 3.3.1:
Code: Pascal  [Select][+][-]
  1. program helpers3;
  2. {$if fpc_fullversion < 30000}{$error this program needs 3.0.0 or higher}{$endif}
  3. {$mode objfpc}{$H+}
  4. {$modeswitch typehelpers}
  5. uses sysutils;
  6. type
  7.   TMyIntegerHelper = type helper for integer
  8.   procedure inc(const num:integer = 1);
  9.   end;
  10.  
  11.   procedure TMyIntegerHelper.inc(const num:integer);
  12.   begin
  13.     system.inc(self,num);
  14.   end;
  15.  
  16. var
  17.   i:integer = 100;
  18. begin
  19.   i.inc;
  20.   writeln(i);
  21.   i.inc(99);
  22. end.
This compiles from version 3.0.0
So if you have a version below trunk, you must use this version and only one helper can be in scope.
« Last Edit: October 07, 2024, 01:56:33 pm by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

carl_caulkett

  • Hero Member
  • *****
  • Posts: 594
Re: Helper functions for Integers and other primitive types
« Reply #12 on: October 07, 2024, 03:06:05 pm »
No worries, I forgot the line {$modeswitch TypeHelpers}.
"It builds... ship it!"

carl_caulkett

  • Hero Member
  • *****
  • Posts: 594
Re: Helper functions for Integers and other primitive types
« Reply #13 on: October 07, 2024, 03:08:25 pm »
Demo inherited helpers:

Thanks, Thaddy. That's brilliant help 🙏🏽

I'm not explicitly on trunk for either Lazarus or FPC, but both inherited and multi seem to work fine  :D
« Last Edit: October 07, 2024, 03:16:12 pm by carl_caulkett »
"It builds... ship it!"

 

TinyPortal © 2005-2018