Recent

Author Topic: Class Functions in Helper Types  (Read 1856 times)

Handoko

  • Hero Member
  • *****
  • Posts: 5154
  • My goal: build my own game engine using Lazarus
Class Functions in Helper Types
« on: April 23, 2020, 04:38:38 pm »
While exploring TStringHelper, I noticed some functions are class functions.
https://www.freepascal.org/docs-html/rtl/sysutils/tstringhelper.html

Can anyone tell me why they are class functions not ordinary functions?

eljo

  • Sr. Member
  • ****
  • Posts: 468
Re: Class Functions in Helper Types
« Reply #1 on: April 23, 2020, 05:08:36 pm »
class functions do not require a string variable to be used can be called directly from the string type ee string.compare(str1,str2) non class need to be called from a string variable as a base ee str1.CompareTo(str2); to make it a bit more generic class function do not require a valid self variable non class method require a a valid self parameter.

Handoko

  • Hero Member
  • *****
  • Posts: 5154
  • My goal: build my own game engine using Lazarus
Re: Class Functions in Helper Types
« Reply #2 on: April 23, 2020, 05:22:59 pm »
How to use the non class function as you mentioned? This below can't be compiled:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   i: Integer;
  4. begin
  5.   i := string.Compare('123','abc');
  6. end;
« Last Edit: April 23, 2020, 05:29:00 pm by Handoko »

eljo

  • Sr. Member
  • ****
  • Posts: 468
Re: Class Functions in Helper Types
« Reply #3 on: April 23, 2020, 05:29:29 pm »
its only defined for ansistring not utf8 or unicodeString so something like
Code: Pascal  [Select][+][-]
  1.   ShowMessage(IntToStr(AnsiString.Compare('This is a Test', 'a Test')));
  2.  

Handoko

  • Hero Member
  • *****
  • Posts: 5154
  • My goal: build my own game engine using Lazarus
Re: Class Functions in Helper Types
« Reply #4 on: April 23, 2020, 05:33:13 pm »
But what is the benefit of it? I mean we can simply use CompareStr or CompareText:

Code: Pascal  [Select][+][-]
  1.   ShowMessage(IntToStr(CompareStr('This is a Test', 'a Test')));

Without the self ability in the type helper, those functions are just like ordinary string functions:
https://www.freepascal.org/docs-html/rtl/sysutils/stringfunctions.html
« Last Edit: April 23, 2020, 05:36:31 pm by Handoko »

eljo

  • Sr. Member
  • ****
  • Posts: 468
Re: Class Functions in Helper Types
« Reply #5 on: April 23, 2020, 05:36:13 pm »
I have no idea, I assume its the modern way to not have to memorize units and procedures just type (Type or var). and you get a number of supported process/actions right there.

Other than that I find no value my self.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5481
  • Compiler Developer
Re: Class Functions in Helper Types
« Reply #6 on: April 23, 2020, 05:42:34 pm »
But what is the benefit of it? I mean we can simply use CompareStr or CompareText:

Code: Pascal  [Select][+][-]
  1.   ShowMessage(IntToStr(CompareStr('This is a Test', 'a Test')));

Without the self ability in the type helper, those functions are just like ordinary string functions:
https://www.freepascal.org/docs-html/rtl/sysutils/stringfunctions.html

The idea is to group methods together with their types. This way the IDE can show applicable methods if you just type AnsiString.|.

Compare might be a bad example, but you could think of something like UInt32.GetRandom which would generate a random 32-bit value compared to UInt16.GetRandom which would generate a random 16-bit value. In essence they have the same usability as static class functions/procedures (compared to non-static class methods).

Handoko

  • Hero Member
  • *****
  • Posts: 5154
  • My goal: build my own game engine using Lazarus
Re: Class Functions in Helper Types
« Reply #7 on: April 23, 2020, 05:55:53 pm »
That sounds reasonable but I can't find UInt32.GetRandom.
https://www.freepascal.org/docs-html/rtl/sysutils/tcardinalhelper.html

I like the idea of grouping the methods. But personally I think the are syntactic saccharin, they cause more troubles than useful. Often I will be given a long list of items that are not very useful.

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Class Functions in Helper Types
« Reply #8 on: April 23, 2020, 06:38:06 pm »
The "standard" helpers are mostly just a convenience; there's almost nothing there that can't be done with the corresponding functions in System, SysUtils, etc.

IMHO, their use depends more on preference than on anything else; sometimes you might find more useful or readable to do
Code: Pascal  [Select][+][-]
  1. SomeInt := SomeString.ToInteger;
and others it might seem more intuitive to write
Code: Pascal  [Select][+][-]
  1. SomeInt := StrToInt(SomeString);

Basically, they simply "convert" normal types in "almost-classes" (at least from a sintactical p.o.v)
« Last Edit: April 23, 2020, 06:40:20 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Otto

  • Full Member
  • ***
  • Posts: 226
Re: Class Functions in Helper Types
« Reply #9 on: April 23, 2020, 06:53:31 pm »
I find what lucamar said very fair. I would like to add that for those who use many different programming languages at the same time, the solution given by the “helpers” would be easier to remember; in fact, as PascalDragon has already said, it would be shown by the IDE when writing code.
Kind regards.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5481
  • Compiler Developer
Re: Class Functions in Helper Types
« Reply #10 on: April 24, 2020, 09:36:24 am »
That sounds reasonable but I can't find UInt32.GetRandom.
https://www.freepascal.org/docs-html/rtl/sysutils/tcardinalhelper.html

It was a hypothetical example. I should have made that more clear. :-[

I like the idea of grouping the methods. But personally I think the are syntactic saccharin, they cause more troubles than useful. Often I will be given a long list of items that are not very useful.

But that's the point: if you trigger the identifier completion in a "global" scope you'll get all functions available. If you however trigger the identifier completion after a "." the IDE will restrict the identifiers to those that are available on that type. Thus your "long list of items that are not very useful" will be shorter than it would be otherwise. You'll more easily find the methods that might apply to your situation.

Handoko

  • Hero Member
  • *****
  • Posts: 5154
  • My goal: build my own game engine using Lazarus
Re: Class Functions in Helper Types
« Reply #11 on: April 24, 2020, 10:56:09 am »
Thank you for the explanation. I personally think the list (of helper types) will be better if without those class function items.

 

TinyPortal © 2005-2018