Recent

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

Fibonacci

  • Hero Member
  • *****
  • Posts: 950
  • Behold, I bring salvation - FPC Unleashed
could really use someone else on the "team" (since currently it's just me :( ).
Off topic:  I was under the impression that you and Warfley were the team.  Was that the wrong impression ?

It never really was a team.

At some point I mentioned that @Warfley was in the organization (on GitHub), but his membership wasn't public and there was no visible activity, so there was no real indication of a "team" from the outside.

What actually happened is that I created the fpc-unleashed organization, added both my work and his (unmerged features), and invited him. He accepted, we both had full admin rights, we talked briefly, but there was no real collaboration or ongoing work.

After a longer period of inactivity I removed him from the organization. Partly due to that inactivity, but also because I'm fairly certain we would fundamentally disagree on many of the features I want to implement. Since we both had admin rights, it could easily turn into a situation where one of us starts undoing the other's work - or even removing each other from the organization ;)

That said, @Warfley did an excellent job - Statement Expressions and Record Composition are genuinely impressive work. Unfortunately, the Record Composition feature currently has merge conflicts, so it's on hold. It also breaks IDE autocomplete quite badly, which is a non-trivial issue. In my view, every language feature should come with proper IDE support.

If there is to be actual collaboration in the future, it would have to be more deliberate this time. The original invite was rather casual, and he probably didn't fully know what he was signing up for.
FPC Unleashed - inline vars, tuples, statement expressions, array equality, compound assignments, indexed/lazy labels, no-RTTI & more. ⭐ Star it on GitHub!

440bx

  • Hero Member
  • *****
  • Posts: 6491
Thank you for clarifying the situation Fibonacci.
FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

ALLIGATOR

  • Sr. Member
  • ****
  • Posts: 417
  • I use FPC [main] 💪🐯💪
Quote from: Fibonacci
In my view, every language feature should come with proper IDE support.
💯👍
I may seem rude - please don't take it personally

xiyi0616

  • Jr. Member
  • **
  • Posts: 64


Good news: I already have this implemented. It's sitting in the feat-strinterp branch, waiting to be published (or not). It may be available in FPC Unleashed under the INTERPOLATEDSTRINGS modeswitch, or just unleashed mode. I just rebased it to the devel, so you can merge it yourself without conflicts and play with it :)

I don't publish everything I write - I'm not even sure if this feature is "worthy enough" to be included in FPC Unleashed. Either way, publishing a feature takes time - documentation, examples, explaining things to people, answering questions, potential bugfixes. And I'd rather write code than deal with people - could really use someone else on the "team" (since currently it's just me :( ).

Anyway, next to be published is the tuples feature. It's already in the devel branch on GitHub and available for testing. I'll be moving it to main soon and announcing it.

Awesome! In the age of AI, we humans should focus more on logic, as constantly analyzing parameter formats like those in Format, along with the reading inconvenience they cause, demands a bit of extra mental effort—however minimal. With our limited "cognitive power" and energy, it’s better to direct as much of it as possible toward what truly matters.

Zvoni

  • Hero Member
  • *****
  • Posts: 3376
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.');

The IntToStr (and similar the .ToString) are superfluous. Write(Ln) can handle integers just fine, no need to convert them to string.

Bart
That was for demonstration purposes.
You would need IntToStr/ToString, when e.g. assigning to a String-variable
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

creaothceann

  • Sr. Member
  • ****
  • Posts: 361
interpolated strings

Not a feature request, I'm just curious if this would work?

Code: Pascal  [Select][+][-]
  1. procedure Test(const s : string);
  2. begin
  3.         WriteLn($'\{s} = {s}');  // Test('abc') --> '{s} = abc'
  4. end;

Thaddy

  • Hero Member
  • *****
  • Posts: 19165
  • Glad to be alive.
I suggest one code use the Lazarus macro convention of {%some_place_holder} instead for substitutions. This makes it more consistent.
objects are fine constructs. You can even initialize them with constructors.

Thausand

  • Hero Member
  • *****
  • Posts: 536
That was for demonstration purposes.
You would need IntToStr/ToString, when e.g. assigning to a String-variable
https://www.freepascal.org/docs-html/rtl/system/writestr.html
A docile goblin always follow HERMES.md

Thaddy

  • Hero Member
  • *****
  • Posts: 19165
  • Glad to be alive.
Zvoni knows that write/str/ln supports that, he maybe forgot.

The same write family also supports formatting, though not as extensive as Format() and family.
Write etc are intrinsics, therefor often more efficient and do not rely on sysutils.

But note some of the features of the write family are not Delphi compatible: Freepascal is way more capable. That may confuse you when you use it in Delphi too.
(e.g. Fpc can write more types)
« Last Edit: April 14, 2026, 05:13:30 pm by Thaddy »
objects are fine constructs. You can even initialize them with constructors.

Fibonacci

  • Hero Member
  • *****
  • Posts: 950
  • Behold, I bring salvation - FPC Unleashed
interpolated strings

Not a feature request, I'm just curious if this would work?

Code: Pascal  [Select][+][-]
  1. procedure Test(const s : string);
  2. begin
  3.         WriteLn($'\{s} = {s}');  // Test('abc') --> '{s} = abc'
  4. end;

No. Escaping works with double braces, like so:

Code: Pascal  [Select][+][-]
  1. WriteLn($'{{s}} = {s}');  // Test('abc') --> '{s} = abc'



Inside braces you can put literally any expression - here's a wild one:

Code: Pascal  [Select][+][-]
  1. writeln($'Result: {case MessageBoxA(0, 'Is it wild?', 'Question', MB_YESNO or MB_ICONQUESTION) of
  2.  6: 'Absolutely!';
  3.  7: 'Nope.';
  4.  else 'no idea'
  5. }');
« Last Edit: April 15, 2026, 03:33:21 am by Fibonacci »
FPC Unleashed - inline vars, tuples, statement expressions, array equality, compound assignments, indexed/lazy labels, no-RTTI & more. ⭐ Star it on GitHub!

Zvoni

  • Hero Member
  • *****
  • Posts: 3376
Zvoni knows that write/str/ln supports that, he maybe forgot.

The same write family also supports formatting, though not as extensive as Format() and family.
Write etc are intrinsics, therefor often more efficient and do not rely on sysutils.

But note some of the features of the write family are not Delphi compatible: Freepascal is way more capable. That may confuse you when you use it in Delphi too.
(e.g. Fpc can write more types)
I didn't forget.

I was writing ".... e.g. ASSIGNING to a String-Variable".... --> NOT calling a Procedure (self-written, intrinsic, whatever), to do the assignment for you

Aircode
Code: Pascal  [Select][+][-]
  1. Var  
  2.    s, Name:String;
  3.    Age:Integer;
  4. //....
  5. Begin
  6.   Name:='Zvoni';
  7.   Age:=12; //Yeah...Laugh it up :) Not telling you my Age
  8.   s:='Hi. My Name is {name} and i am {age} years old';
  9. End;
This illustrates, why i hope this bulls*** never gets introduced into FPC

OTOH, if it does get introduced to FPC, i don't have to use it *shrug*
but in that case, don't ask me for Code-Review, because i would be staring at it, and it wouldn't make a jot of sense to me
« Last Edit: April 15, 2026, 09:15:56 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

Khrys

  • Sr. Member
  • ****
  • Posts: 439
Code: Pascal  [Select][+][-]
  1. Var  
  2.    s, Name:String;
  3.    Age:Integer;
  4. //....
  5. Begin
  6.   Name:='Zvoni';
  7.   Age:=12; //Yeah...Laugh it up :) Not telling you my Age
  8.   s:='Hi. My Name is {name} and i am {age} years old';
  9. End;
This illustrates, why i hope this bulls*** never gets introduced into FPC

What exactly is this supposed to illustrate?

Zvoni

  • Hero Member
  • *****
  • Posts: 3376
Code: Pascal  [Select][+][-]
  1. Var  
  2.    s, Name:String;
  3.    Age:Integer;
  4. //....
  5. Begin
  6.   Name:='Zvoni';
  7.   Age:=12; //Yeah...Laugh it up :) Not telling you my Age
  8.   s:='Hi. My Name is {name} and i am {age} years old';
  9. End;
This illustrates, why i hope this bulls*** never gets introduced into FPC

What exactly is this supposed to illustrate?
As a programmer i write INSTRUCTIONS, telling the code what to do.
In above sample, i don't see an INSTRUCTION to Replace the 2 "placeholders" with the values of the 2 variables, one of which has to be typecast to string first
I would be relying on some black-boxed voodoo and black magic

Reading the following piece of code i know/can see exactly what's happening
Code: Pascal  [Select][+][-]
  1. Var  
  2.    s,t, Name:String;
  3.    Age:Integer;
  4. //....
  5. Begin
  6.   Name:='Zvoni';
  7.   Age:=12; //Yeah...Laugh it up :) Not telling you my Age
  8.   s:='Hi. My Name is '+Name+' and i am '+Age.ToString+' years old';
  9.   t:=Format('Hi. My Name is %s and i am %d years old',[name,age]);
  10. End;

Call me "old-fashioned"... *shrug*
« Last Edit: April 15, 2026, 09:39:48 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

creaothceann

  • Sr. Member
  • ****
  • Posts: 361
The instruction is the $ prefix.

Zvoni

  • Hero Member
  • *****
  • Posts: 3376
The instruction is the $ prefix.
Be my guest to use it......
I'm definitely out of here
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

 

TinyPortal © 2005-2018