Recent

Author Topic: TryStrToInt declaration question  (Read 7917 times)

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
Re: TryStrToInt declaration question
« Reply #15 on: January 19, 2022, 02:01:26 pm »
   I've looked in the rtl documentation I have and I've seen the following declarations:
Code: Pascal  [Select][+][-]
  1. function IntToStr(Value: LongInt) : string
  2. function IntToStr(Value: Int64) : string
  3. function IntToStr(Value: QWord) : string
  4. function UIntToStr(Value: QWord) : string
  5. function UIntToStr(Value: Cardinal) : string
   Maybe function IntToStr(Value: QWord) : string does the same thing as function UIntToStr(Value: QWord) : string, I don't know.

The UIntToStr overloads were added for Delphi compatibility.

   In addition to the fact that we have TryStrToFloat working with single, double and extended, the point presenting the above extras is that we have IntToStr functions working with 64bit integers, but we don't have TryStrToInt working with 64bit integers. This can't be good. There is no reason or hint to realize that contrary to the use of TryStrToFloat and IntToStr, a developer must use different function names when he wants to do what he expects from a TryStrToInt function.

You can just as well use the following, then you don't need to deal with the differences between TryStrToInt and TryStrToInt64:

Code: Pascal  [Select][+][-]
  1. {$mode objfpc}{$H+}
  2.  
  3. uses
  4.   SysUtils;
  5.  
  6. var
  7.   s: SizeInt;
  8. begin
  9.   if SizeInt.TryParse('8481849', s) then
  10.     Writeln(s)
  11.   else
  12.     Writeln('Invalid number');
  13. end.

lagprogramming

  • Sr. Member
  • ****
  • Posts: 405
Re: TryStrToInt declaration question
« Reply #16 on: January 19, 2022, 03:22:05 pm »
Funny stuff. I've decided to write a new TryToWhateverInteger function and to my surprise I've found that almost 10 years ago I've written a TryToLongword function. This means that almost a decade a ago I've hit the same problem the only difference being that at that time the problem I've had was with 32bit unsigned integers and now with 64bit integers.  :-[ History repeating.

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: TryStrToInt declaration question
« Reply #17 on: January 19, 2022, 03:51:55 pm »
The problem being?

Bart

lagprogramming

  • Sr. Member
  • ****
  • Posts: 405
Re: TryStrToInt declaration question
« Reply #18 on: January 19, 2022, 05:15:52 pm »
Code: Pascal  [Select][+][-]
  1. function ErrorCompilingThis(const StringParam:string; out LongwordParam:longword):boolean;
  2. begin
  3. //Error at the following line: Call by var for arg no. 2 has to match exactly: Got "LongWord" expected "LongInt"
  4. result:=TryStrToInt(StringParam,LongwordParam);
  5. end;
  6.  
  7. procedure TForm1.Button1Click(Sender: TObject);
  8. var
  9.   LW1,LW2:LongWord;
  10. begin
  11. LW1:=LongWord(not(0));LW2:=0;
  12. //Obviously, same error in the following line
  13. if TryStrToInt(IntToStr(LW1),LW2) THEN
  14.    if LW1=LW2 then Form1.Caption:='OK';
  15. end;

   Now I see there is a TryStrToDWord function, probably it existed even at the time I've written my own TryStrToLongword. Totally counter-intuitive names in the rtl, really.

   Nowadays we have a generic function name TryStrToInt that supports multiple types of strings but when it comes to the integer parameter we're stuck with the longint one. In addition to this function name we have a dozen other functions but with precise names that do the same kind of thing as TryStrToInt and that support the same multiple types of strings but only one type of integer: TryStrToInt64, TryStrToQWord, TryStrToUInt64, TryStrToDWord and who knows how many others. If the function name has to identify the second parameter then TryStrToInt doesn't identify the LongInt parameter, it confuzes people into thinking that it's a function that applies to all sorts of integers like it applies to all sorts of strings.

   I've given the TryStrToFloat example, here is another one:
   TryStrToBool(const S: string; out Value: Boolean) : Boolean;
   TryStrToBool(const S: string; out Value: Boolean; const FormatSettings: TFormatSettings) : Boolean;
   The rtl doesn't have a TryStrToBool(const S: string; out Value: BYTEBOOL) : Boolean; declaration, neither a TryStrToLONGBOOL(const S: string; out Value: LONGBOOL) : Boolean declaration.

   Probably due to historical reasons we have a function with a generic name that should cover many types of strings and integers, but it doesn't. At least I've been mislead by the name.

ASBzone

  • Hero Member
  • *****
  • Posts: 678
  • Automation leads to relaxation...
    • Free Console Utilities for Windows (and a few for Linux) from BrainWaveCC
Re: TryStrToInt declaration question
« Reply #19 on: February 08, 2022, 11:05:37 pm »
Regarding the "TryStrTo..." functions, we have a single function name to convert strings to single, double or extended, it's TryStrToFloat.
Thank you!

I had an application that needed several numeric conversions, and at first I just used TryStrToFloat, and typecast to the appropriate integer type when necessary.

Yeah...  That was noticeably slower than staying within the fun realm of integers for integer results.
-ASB: https://www.BrainWaveCC.com/

Lazarus v2.2.7-ada7a90186 / FPC v3.2.3-706-gaadb53e72c
(Windows 64-bit install w/Win32 and Linux/Arm cross-compiles via FpcUpDeluxe on both instances)

My Systems: Windows 10/11 Pro x64 (Current)

ASBzone

  • Hero Member
  • *****
  • Posts: 678
  • Automation leads to relaxation...
    • Free Console Utilities for Windows (and a few for Linux) from BrainWaveCC
Re: TryStrToInt declaration question
« Reply #20 on: February 08, 2022, 11:19:27 pm »
In addition to this function name we have a dozen other functions but with precise names that do the same kind of thing as TryStrToInt and that support the same multiple types of strings but only one type of integer: TryStrToInt64, TryStrToQWord, TryStrToUInt64, TryStrToDWord and who knows how many others.

It seems straightforward to me why you can have a single name for a myriad of functions when converting from IntegerX to String.  You're only looking for a single output type, and the parameter is going to be matched within the overloaded functions, so only one name is needed.

However, if you are going to turn a single string into *some* type of Integer, how will the computer know what kind of Integer you wanted to end up with?  You might have specific reasons for asking for Int32 even if using a QWord variable.   How could this ever be easily communicated without a specific function name?   
-ASB: https://www.BrainWaveCC.com/

Lazarus v2.2.7-ada7a90186 / FPC v3.2.3-706-gaadb53e72c
(Windows 64-bit install w/Win32 and Linux/Arm cross-compiles via FpcUpDeluxe on both instances)

My Systems: Windows 10/11 Pro x64 (Current)

 

TinyPortal © 2005-2018