Recent

Author Topic: I hope FreePascal can support syntax like Python's f'string {variable}' or ...  (Read 4079 times)

xiyi0616

  • Jr. Member
  • **
  • Posts: 64
I hope FreePascal can support syntax like Python's f'string {variable}' or JavaScript's ${variable}for string variable interpolation.

Post in the wrong section?

xiyi0616

Handoko

  • Hero Member
  • *****
  • Posts: 5537
  • My goal: build my own game engine using Lazarus
I don't know much about f string but, Format seems similar to what you want:

https://www.freepascal.org/docs-html/rtl/sysutils/format.html

Thaddy

  • Hero Member
  • *****
  • Posts: 19129
  • Glad to be alive.
No this is a good place and yes, FreePascal supports the concept in its different format functions.

Example:
Code: Pascal  [Select][+][-]
  1. program fillin;
  2. {$mode objfpc}
  3. uses sysutils;
  4.  
  5. var
  6.    name:string  = 'Bart Simpson';
  7.    age:integer = 48;
  8.    Res:string;
  9. begin
  10.   writeln(Format('This record contains the name %s and an age of %d',[name,age]));  // uses sysutils
  11. //or
  12.   writeln('This record contains the name ',name, ' and an age of ', age);//
  13. // or
  14.   writestr(Res,'This record contains the name ',name, ' and an age of ', age);  
  15.   writeln(Res);
  16. uses no extra units
  17. end.
Both Format and Write/Writeln/Writestr have many format modifiers for the format specifiers to control the output.
There are many ways to write templated code in FreePascal. Not just 1.
« Last Edit: April 13, 2026, 07:29:20 am by Thaddy »
objects are fine constructs. You can even initialize them with constructors.

Handoko

  • Hero Member
  • *****
  • Posts: 5537
  • My goal: build my own game engine using Lazarus

xiyi0616

  • Jr. Member
  • **
  • Posts: 64
Thanks for the detailed explanation. I was aware of Format, but from the perspective of ease of use and code readability, I just think the approach in Python and JavaScript is quite good. It’s merely a suggestion.

Khrys

  • Sr. Member
  • ****
  • Posts: 434
Sadly in Pascal the only options are slow & error-prone format strings or inflexible & cumbersome  Write*  builtins.
Your best bet would be to make a suggestion for @Fibonacci in this thread.

xiyi0616

  • Jr. Member
  • **
  • Posts: 64
@Fibonacci Can FreePascal support syntax like Python's f'string {variable}' or JavaScript's ${variable}for string variable interpolation? Make FreePascal better (great)!  :)
« Last Edit: April 13, 2026, 10:07:21 am by xiyi0616 »

440bx

  • Hero Member
  • *****
  • Posts: 6472
Sadly in Pascal the only options are slow & error-prone format strings or inflexible & cumbersome  Write*  builtins.
I've never attempted to compare the performance of FPC output facilities with those of Python but, I find it a bit difficult to believe that interpreted I/O functions would be faster than compiled I/O code.

In addition to that, I can't think of any case where Python f'string can do something that can't be fairly easily done with WriteStr. 

Seems to me there is nothing that can be done with one that cannot be done with the other. Since I'm very far from being a Python expert, correct me if I'm wrong and, in that case, one or more examples of those cases would be great.
FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

Zvoni

  • Hero Member
  • *****
  • Posts: 3367
I hope FreePascal can support syntax like Python's f'string {variable}' or JavaScript's ${variable}for string variable interpolation.
....and i hope it never comes......
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

xiyi0616

  • Jr. Member
  • **
  • Posts: 64
Sadly in Pascal the only options are slow & error-prone format strings or inflexible & cumbersome  Write*  builtins.
I've never attempted to compare the performance of FPC output facilities with those of Python but, I find it a bit difficult to believe that interpreted I/O functions would be faster than compiled I/O code.

In addition to that, I can't think of any case where Python f'string can do something that can't be fairly easily done with WriteStr. 

Seems to me there is nothing that can be done with one that cannot be done with the other. Since I'm very far from being a Python expert, correct me if I'm wrong and, in that case, one or more examples of those cases would be great.

I'm not an expert, so I'm just speaking from a user's perspective. With the Formatapproach, the parameters inside the brackets have to be written in sequence according to the placeholders in the preceding format string. If you add too many, too few, or mess up the order, it's not easy to spot when the variable types are the same. In terms of readability, that's indeed quite poor. Formatis just a function with a certain syntax, and you constantly have to cross-reference between the placeholders and the variables—it's nowhere near as readable as f-strings.

xiyi0616

  • Jr. Member
  • **
  • Posts: 64
I hope FreePascal can support syntax like Python's f'string {variable}' or JavaScript's ${variable}for string variable interpolation.
....and i hope it never comes......
Reason?  Maybe you could enlighten me?

440bx

  • Hero Member
  • *****
  • Posts: 6472
Formatis just a function with a certain syntax, and you constantly have to cross-reference between the placeholders and the variables—it's nowhere near as readable as f-strings.
That's true but, that problem is not present in WriteStr and WriteStr is fairly capable, likely as capable as Python's f'string (but I could be wrong about that... I'm no expert in Python.)
FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

Zvoni

  • Hero Member
  • *****
  • Posts: 3367
I hope FreePascal can support syntax like Python's f'string {variable}' or JavaScript's ${variable}for string variable interpolation.
....and i hope it never comes......
Reason?  Maybe you could enlighten me?

because something like this
Code: Python  [Select][+][-]
  1. >>> name = "Jane"
  2. >>> age = 25
  3.  
  4. >>> f"Hello, {name}! You're {age} years old."
  5. 'Hello, Jane! You're 25 years old.'
has an implicit Typecast (Age is Integer), and Pascal as a strongly typed Language.....

What's wrong with
Code: Pascal  [Select][+][-]
  1. Writeln('Hello, '+Name+'! You''re '+IntToStr(Age)+' years old.');
  2.  
  3. {Needs Sysutils in Uses}
  4. Writeln('Hello, '+Name+'! You''re '+Age.ToString+' years old.');
or write your own Operator-Overload for "+", concating String with Integer, and Integer with String

All said: Implicit TypeCasts have been the bane of many Programmers.....
« Last Edit: April 13, 2026, 11:12:05 am by Zvoni »
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

xiyi0616

  • Jr. Member
  • **
  • Posts: 64
Formatis just a function with a certain syntax, and you constantly have to cross-reference between the placeholders and the variables—it's nowhere near as readable as f-strings.
That's true but, that problem is not present in WriteStr and WriteStr is fairly capable, likely as capable as Python's f'string (but I could be wrong about that... I'm no expert in Python.)
But my point is, I’m not actually using WriteStr. The same issue applies in other statements too. For example, Edit1.Text = f'{userName} has done.';The f-string syntax is applied consistently across all the contexts where it’s needed.

xiyi0616

  • Jr. Member
  • **
  • Posts: 64
@Zvoni

Write freepascal like this may be nice (do not care var type, inttostr(), timetostr() ? no ):
 Edit1.Text = f'{userName} has done.';
« Last Edit: April 13, 2026, 11:18:15 am by xiyi0616 »

 

TinyPortal © 2005-2018