Class Functions in Helper Types lazarus / 62-General ---------------------------------------------------------------------- Handoko · 2020-04-23 14:38 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 · 2020-04-23 15:08 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 · 2020-04-23 15:22 How to use the non class function as you mentioned? This below can't be compiled: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Button1Click(Sender: TObject);var  i: Integer;begin  i := string.Compare('123','abc');end; ---------------------------------------------------------------------- eljo · 2020-04-23 15:29 its only defined for ansistring not utf8 or unicodeString so something like Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   ShowMessage(IntToStr(AnsiString.Compare('This is a Test', 'a Test'))); ---------------------------------------------------------------------- Handoko · 2020-04-23 15:33 But what is the benefit of it? I mean we can simply use CompareStr or CompareText: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   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 ---------------------------------------------------------------------- eljo · 2020-04-23 15:36 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 · 2020-04-23 15:42 Quote from: Handoko on April 23, 2020, 05:33:13 pmBut what is the benefit of it? I mean we can simply use CompareStr or CompareText: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   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 · 2020-04-23 15:55 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 · 2020-04-23 16:38 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i SomeInt := SomeString.ToInteger;and others it might seem more intuitive to write Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i SomeInt := StrToInt(SomeString); Basically, they simply "convert" normal types in "almost-classes" (at least from a sintactical p.o.v) ---------------------------------------------------------------------- Otto · 2020-04-23 16:53 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. ---------------------------------------------------------------------- PascalDragon · 2020-04-24 07:36 Quote from: Handoko on April 23, 2020, 05:55:53 pmThat 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. Quote from: Handoko on April 23, 2020, 05:55:53 pmI 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 · 2020-04-24 08:56 Thank you for the explanation. I personally think the list (of helper types) will be better if without those class function items.