Automatic conversion to string lazarus / 21-General ---------------------------------------------------------------------- Curt Carpenter · 2025-02-01 21:32 Suppose I have a number of values v1,v2,...,vN that I want to format into a string S -- say a comma-separated string.  I can do this  in many cases like this: 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     S := v1.ToString+','+v2.ToString+','+...+','+vN.ToString; How hard would it be though to make the compiler, knowing that S is a string variable on the left of the assignment, automatically try to ensure that everything on the right (v1,v2,...,vN) is converted into string form, or issue an exception if such a conversion were not possible? Not advocating for this, just curious what the downside would be to having the compiler do such a thing. ---------------------------------------------------------------------- Wilko500 · 2025-02-01 21:46 I don't have the answer but I have often wondered why ShowMessage can only take strings but WriteLn can take strings and integers and do thenevessary conversions. ---------------------------------------------------------------------- TRon · 2025-02-01 22:02 Use WriteStr or Format otherwise you would have to dig into operator overloading (in case possible). Quote from: Wilko500 on February 01, 2025, 10:46:06 pmI don't have the answer but I have often wondered why ShowMessage can only take strings but WriteLn can take strings and integers and do thenevessary conversions. Because Write is an internal compiler function. ---------------------------------------------------------------------- dseligo · 2025-02-01 22:06 Quote from: Curt Carpenter on February 01, 2025, 10:32:14 pmSuppose I have a number of values v1,v2,...,vN that I want to format into a string S -- say a comma-separated string.  I can do this  in many cases like this: 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     S := v1.ToString+','+v2.ToString+','+...+','+vN.ToString; How hard would it be though to make the compiler, knowing that S is a string variable on the left of the assignment, automatically try to ensure that everything on the right (v1,v2,...,vN) is converted into string form, or issue an exception if such a conversion were not possible? Not advocating for this, just curious what the downside would be to having the compiler do such a thing. Maybe you can use WriteStr (https://www.freepascal.org/docs- html/rtl/system/writestr.html). ---------------------------------------------------------------------- TBMan · 2025-02-02 01:27 The writestr looks interesting. I'm old school and I should look into the newer stuff. That beats doing a series of str() and using multiple variables to make one string. ---------------------------------------------------------------------- egsuh · 2025-02-02 08:49 Well, Pascal is type-strict language. I have written following function, and this works. 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 function CommaStr(Args: array of const): string;var   arg: TVarRec;begin   Result := '';   for arg in Args do begin      case arg.VType of       vtInteger: Result += IntToStr(arg.VInteger);         vtString: Result += arg.VString^;         vtAnsiString: Result += string(arg.VAnsiString);      end;      Result += ',';   end;   RemovePadChars(Result, [',']);end; ---------------------------------------------------------------------- paweld · 2025-02-02 09:04 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 S := Format('%d,%d,...,%d', [v1, v2, ..., vN]);more info: https://lazarus- ccr.sourceforge.io/docs/rtl/sysutils/format.html ---------------------------------------------------------------------- Thaddy · 2025-02-02 10:43 Quote from: TBMan on February 02, 2025, 02:27:54 amThe writestr looks interesting. I'm old school and I should look into the newer stuff. That beats doing a series of str() and using multiple variables to make one string. yes, writestr is often a good option: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 {$mode objfpc}{$H+}var   V1:double = 12.34;   V2:integer = 10; // optionally formats with a double':'   V3:string = 'Hello';  // optionally formats length with a single ':'   S:string;begin   writestr(S, V1:4:4,V2:4, V3:6);   writeln(s);end.It has some limitations over the formatxxx functions, but it does not need sysutils. Errror checking can best be done in {$I-} state and examining IOResult. Writestr is not new - actually very old - but was only specified in the ISO Extended Pascal standard. In Freepascal it is available in all modes. ---------------------------------------------------------------------- Curt Carpenter · 2025-02-02 15:06 Appreciate all the approaches, but my curiosity was really about why the compiler couldn't do the conversion of the elements on the right of the assignment .ToString  automatically (or throw an exception if one of the elements is not convertable).  Strictly curiosity -- suspect there's a problem with the idea. ---------------------------------------------------------------------- Thaddy · 2025-02-02 15:23 Well, the compiler can, or better the rtl can. You just need to include the sysutils unit for a wealth of conversions. Note that from a compiler point of view, you can not expect type inference for everything, but there are some tricks in fpc - not delphi - that I will add here later.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 {$mode objfpc}{$H+}uses   sysutils;// assign a string to an integer. string must be valid integer representationoperator := (const b:string):integer;inline;begin  result := b.tointeger;end; // assign an integer to a stringoperator := (const b:integer):string;inline;begin  result := b.tostring;end; begin  writeln('100'.ToInteger);  writeln(100.ToString);end.As long as the string can be represented as an integer it becomes an integer on assignment The integer will be simply represented as a string on assignment. This can be done for almost all and any types. So, yes, as the example proves, Freepascal supports type inference, but not all conversions are out of the box. You have to put in some work for now. ---------------------------------------------------------------------- PascalDragon · 2025-02-02 19:42 Quote from: Curt Carpenter on February 01, 2025, 10:32:14 pmHow hard would it be though to make the compiler, knowing that S is a string variable on the left of the assignment, automatically try to ensure that everything on the right (v1,v2,...,vN) is converted into string form, or issue an exception if such a conversion were not possible? The result of an expression in Pascal does NOT depend on what its assigned to. Unless you have a single element assignment which results in the invocation of an assignment operator or the assignment of an overloaded method/function to a method/function variable the right side of an assignment has no knowledge about what the left side. You can only do this by using a "seed element" (which triggers a conversion to the correct type while parsing from left to right) and assigment and addition operator overloads: 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 program topovld; {$mode objfpc}{$H+} uses  SysUtils; operator := (aArg: LongInt): String;begin  Result := IntToStr(aArg);end; operator + (const aLeft: String; aRight: LongInt): String;begin  Result := aLeft + ' ' + IntToStr(aRight);end; var  s: String;  v1, v2, v3: LongInt;begin  v1 := 42;  v2 := 21;  v3 := 84;   s := '' + v1 + v2 + v3;  Writeln(s);end. ---------------------------------------------------------------------- n7800 · 2025-02-02 20:23 It is worth remembering that Format and WriteStr calls are very slow, since they perform parameter parsing. So calling Str directly (which is called by IntToStr (which is called by Integer.ToString)) will be the fastest: 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 Function TORDINALHELPER.ToString: string; overload; inline;begin  Result:=IntToStr(Self);end;...function IntToStr(Value: QWord): string;begin  System.Str(Value, result);end ;  This is very noticeable if you generate, for example, a large CSV file. ---------------------------------------------------------------------- Curt Carpenter · 2025-02-02 22:08 Quote from: PascalDragon on February 02, 2025, 08:42:21 pmThe result of an expression in Pascal does NOT depend on what its assigned to. I understand "does not" but wonder if there is good reasons why it "could not" or "should not."   Just curious.  I am imagining a "compiler" that, rather than telling me I've made an error and inviting me to correct it, might ask me what I meant in certain circumstances, as in assigning a number to a string. ---------------------------------------------------------------------- PascalDragon · 2025-02-04 21:08 Quote from: Curt Carpenter on February 02, 2025, 11:08:59 pmQuote from: PascalDragon on February 02, 2025, 08:42:21 pmThe result of an expression in Pascal does NOT depend on what its assigned to. I understand "does not" but wonder if there is good reasons why it "could not" or "should not." Because then it's not Pascal.