Author Topic: Unleashed Pascal (async/await, parallel for, match, string interpolation & more)  (Read 70353 times)

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 12820
  • Debugger - SynEdit - and more
    • wiki
That's exactly why throw-away variables like loop counters, whose values can't be used after the loop anyway, should be declared in the loop.
It is so nice to see that at least one person understands the obvious. :)
[/quote]

By that rule... And I am setting "value gone" equal to not used anymore, which may make it gone if it was in a register...

By that rule, there would be no locals left, in some code you have 100% throw away. There are very few cases were all locals are used on the very last line of code. Most stop before...

And quite often they are obvious short lived. Just a temp to hold a function result for maybe 2 or 3 lines of code to come, and then no longer used.

But, if (for arguments sake) I take the requested approach that every var should have a scope matching exactly its values life time, then you need a new scope marker. Because those scopes will **not** be cleanly nested, they will randomly overlap.

How to solve that?

And NO, that isn't a: Don't do anything if you can't do it to absolute perfection <= That is not what I said.



And again, in the spirit of the original post: I am happy for those who want to use them.

To summarize: There are reasons why they should not be... Those were exchanged, and disagreed on. The end of the story (in previous discussions) often was, if they are added to official FPC, then everyone will be forced to deal with it, because eventually you come across 3rd party code that has them.

That last part is now resolved. Code written for FPC will not have them. So that is good enough for me. Code for Delphi already has them, code for the unleashed will have them... Ok, fine I can live with that. Doesn't impact me. I have my choice, everyone else can have theirs. great.

And well, as for the rest we now actually may be getting a measure (Albeit biased by other implications) what the majority wants. We will see in the next "however many" years how many people will switch. If we end up with all 3rd party code only working on unleashed, well then that is a majority thing.

The great thing is, someone took the advice and put their money where their mouth is. They created a fork.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 12820
  • Debugger - SynEdit - and more
    • wiki
The "mechanism" stays the same: First "Hit" is executed, rest is ignored (since no Fallthrough).

"First hit" is not the same. Currently it is "Only hit (if any exists)"

"first hit" implies that their can be duplicate labels. But the current enforcement of no duplicate labels, is one of the big plus points for "case".

This is why I stated earlier, its not "case" (not in Pascal). It is an entirely new construct. It really should have a different keyword.



As I stated, for those who want those new feature (despite whatever else was said), at least consider them beyond the mere surface.

Well, don't if you don't want, really not my problem. I don't joined this thread to deny you what you put in the fork (so a few posts got away). I wanted to add an outsiders view, to maybe give you more ideas on how to get the best of it within chosen target.


Zvoni

  • Hero Member
  • *****
  • Posts: 3603
The "mechanism" stays the same: First "Hit" is executed, rest is ignored (since no Fallthrough).

"First hit" is not the same. Currently it is "Only hit (if any exists)"

"first hit" implies that their can be duplicate labels. But the current enforcement of no duplicate labels, is one of the big plus points for "case".

This is why I stated earlier, its not "case" (not in Pascal). It is an entirely new construct. It really should have a different keyword.



As I stated, for those who want those new feature (despite whatever else was said), at least consider them beyond the mere surface.

Well, don't if you don't want, really not my problem. I don't joined this thread to deny you what you put in the fork (so a few posts got away). I wanted to add an outsiders view, to maybe give you more ideas on how to get the best of it within chosen target.

Agreed.
But for this scenario: First hit. Period

Code: Pascal  [Select][+][-]
  1. Var i:Integer;
  2. i:=5;
  3. Case True of
  4. (i>0) : Writeln('i>0');  //Will execute, and exit the case-block
  5. (i=5) : Writeln('i=5');  //Will not execute
  6. End;
  7.  

Standard-Behavior: No Fallthrough
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

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 12820
  • Debugger - SynEdit - and more
    • wiki
Agreed.
But for this scenario: First hit. Period

Code: Pascal  [Select][+][-]
  1. Var i:Integer;
  2. i:=5;
  3. Case True of
  4. (i>0) : Writeln('i>0');  //Will execute, and exit the case-block
  5. (i=5) : Writeln('i=5');  //Will not execute
  6. End;
  7.  

As I said, if it is added or not, well not my concern.

But if you care for any advice: chose a different keyword.
Code: Pascal  [Select][+][-]
  1. Chose True of
  2. (i>0) : Writeln('i>0');  //Will execute, and exit the case-block
  3. (i=5) : Writeln('i=5');  //Will not execute
  4. End;
  5.  

Alternatives:
- Chose ... in
- Search ... in
- Find ... in
- First ... of

Fibonacci

  • Hero Member
  • *****
  • Posts: 1125
  • Behold, I bring salvation - Unleashed Pascal
    • fibo.gg
But if you care for any advice: chose a different keyword.

I care, but I don't think a new keyword is needed. As I said:

IMO "case true/false of" already signals that labels are expressions, not constants - the user consciously writes a boolean literal instead of a variable, so there's no semantic trap.

Isn't it enough?

First hit. Period

Code: Pascal  [Select][+][-]
  1. Var i:Integer;
  2. i:=5;
  3. Case True of
  4. (i>0) : Writeln('i>0');  //Will execute, and exit the case-block
  5. (i=5) : Writeln('i=5');  //Will not execute
  6. End;
  7.  

You sure? How about this:

Code: Pascal  [Select][+][-]
  1. var i := 5;
  2.  
  3. case true of
  4.   i > 0:       writeln('match 1');             // will execute
  5.   i = 5: begin writeln('match 2'); break; end; // will execute and stop
  6.   i < 7:       writeln('match 3');             // will NOT execute
  7.   else writeln('no match');
  8. end;

All matching labels execute, "break" stops early. match 1 and match 2 execute, match 3 does not. How would you like that served, gentlemen? :)
Unleashed Pascal: async/await, parallel for, match, tuples, string interpolation, inline vars, autofree, no-RTTI & tons more. Star on GitHub ⭐

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 12820
  • Debugger - SynEdit - and more
    • wiki
But if you care for any advice: chose a different keyword.

I care, but I don't think a new keyword is needed. As I said:

IMO "case true/false of" already signals that labels are expressions, not constants - the user consciously writes a boolean literal instead of a variable, so there's no semantic trap.

Isn't it enough?

Depends on how to interpret your example...

That is, I assumed that was an example. I assumed in real live that would work for any expression/type of data.

And even if you really mean only the 2 constant True/False should be allowed (or maybe even only true) then others will want more...)

I am very much for rules how far a feature should extend. But even I am not sure that "only true" is the correct constraint (ignoring that I don't like the feature at all, but not the point)

Code: Pascal  [Select][+][-]
  1. case 42 of
  2. a-b:;
  3. c-d:;
  4. end;
  5. case MyRecord of  // needs an operator for compare
  6. MyRecVar1:;
  7. MyRecVar2:;
  8. end;
  9.  

I already very much dislike the 2nd => because now a custom operator must be executed. That adds possible side effects, and that means there must be a defined order of making the calls, and all of them must be made. No optimizations allowed.

Also for the reader, its very hard to predict which side effects have taken place.... (with pure functions that could be limited)

As an equally worse scenario
Code: Pascal  [Select][+][-]
  1. case value of  // see which function returns the value.
  2. MyFunctionFoo(a,b):;
  3. MyFunctionFoo(c,d):;
  4. MyFunctionBar():;
  5. end;
  6. // or with restriction to true
  7. case TRUE of  // see which function returns the value.
  8. MyFunctionFoo(a,b):;
  9. MyFunctionFoo(c,d):;
  10. MyFunctionBar():;
  11. end;
  12.  

And that really shouldn't be allowed. Even in the "case true of" scenario.  Or do you think its a good idea that case  labels can execute custom user code.



But back to the name....

If it just "case true of" ...  still ambiguous, because it could be
Code: Pascal  [Select][+][-]
  1. case true of
  2. true: ;
  3. false: ; // dead code
  4. end;

"true" is just a value, no different from a boolean variable. That code could exist, if one is testing something and temporarily replaced "case expr of" with "case true of".

And in that case, it should still be clear, that this is the old stile "case" and not the new feature.

Because
Code: Pascal  [Select][+][-]
  1. case true of
  2. true: ;
  3. false: ; // dead code
  4. end;

could be both. It could be "only hit" or "first hit" (ok it does the same in that case).



Zvoni

  • Hero Member
  • *****
  • Posts: 3603
IMO "case true/false of" already signals that labels are expressions, not constants - the user consciously writes a boolean literal instead of a variable, so there's no semantic trap.

Isn't it enough?

First hit. Period

Code: Pascal  [Select][+][-]
  1. Var i:Integer;
  2. i:=5;
  3. Case True of
  4. (i>0) : Writeln('i>0');  //Will execute, and exit the case-block
  5. (i=5) : Writeln('i=5');  //Will not execute
  6. End;
  7.  

You sure? How about this:

Code: Pascal  [Select][+][-]
  1. var i := 5;
  2.  
  3. case true of
  4.   i > 0:       writeln('match 1');             // will execute
  5.   i = 5: begin writeln('match 2'); break; end; // will execute and stop
  6.   i < 7:       writeln('match 3');             // will NOT execute
  7.   else writeln('no match');
  8. end;

All matching labels execute, "break" stops early. match 1 and match 2 execute, match 3 does not. How would you like that served, gentlemen? :)
I'd say: NO. Because that would be basically a Fallthrough, and it would be against the "Standard"-Behavior of the "regular" Case..Of-Construct
Remember: the current "Case..Of" is basically an "If..Then...Else If...Then....Else If..:"-construct: One, and only one Branch will be executed (a missing "Otherwise"-branch not withstanding)
Your proposal is more equal to regular and consecutive "If...Then.." Constructs --> No Else-Branch!!

After thinking about it, i tend to agree with Martin: Maybe a new Keyword (see proposed ones) is really the better way.

I seem to rember, that PascalDragon wrote somewhere, that in the current Case/of-Implementation, the "Labels" must be known at compile-Time --> Constants.
Using the Keywords of "Case True Of" would mean flipping everything upside down:
The constant is now at the Head of the case-Block, and all Labels are expressions that have to be evaluated and then compared.

I think this might really lead to confusion, nevermind how it would influence existing code.
Because in my scenario, True/False is not the only constant/result i can pass to such a Case-Construct
think about following aircode
Code: Pascal  [Select][+][-]
  1. Var i:Integer=5;
  2. ......
  3. Case 8 Of  //a literal constant "8"
  4. (i=5) : Writeln('i=5');  //This should call a compile-Error for the Label! "Integer-Result expected, got Boolean"
  5. i : Writeln('i='+IntToStr(8));   //Flipped around --> Label is an expression--> Will not execute --> "i=5" (from first line) which means 5<>8 --> False --> does not execute
  6. (i+3) : writeln('i+3='+IntToStr(8));  //No Fallthrough, but would execute, since it's the first hit
  7. End;

Yes, i'm complicating things now
« Last Edit: March 20, 2026, 01:47:53 pm 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

440bx

  • Hero Member
  • *****
  • Posts: 6628
How about this:

Code: Pascal  [Select][+][-]
  1. var i := 5;
  2.  
  3. case true of
  4.   i > 0:       writeln('match 1');             // will execute
  5.   i = 5: begin writeln('match 2'); break; end; // will execute and stop
  6.   i < 7:       writeln('match 3');             // will NOT execute
  7.   else writeln('no match');
  8. end;

All matching labels execute, "break" stops early. match 1 and match 2 execute, match 3 does not. How would you like that served, gentlemen? :)
That doesn't work because it can break existing code.  In existing code, if that case statement is in a loop the "break" should cause the exit of the loop, if it doesn't then it has broken the way the code is presumed to be executed.

In Pascal "case" is designed not to fall through.  If fall through is desired then it must be explicitly caused, either by some language keyword that specifies it should or by changing the structure of the code to simulate fall through.

As far as how "case boolean of ..." should work, simple, that feature is described in the COBOL language standard (starting with COBOL85.)  Smart people who know how to design a language have already figured out how it should work and described it in detail (see the EVALUATE statement) and  COBOL's "case" statement (EVALUATE) is multi-variate which greatly simplifies code in situations where actions are determined by a set of multiple conditions.
FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

Fibonacci

  • Hero Member
  • *****
  • Posts: 1125
  • Behold, I bring salvation - Unleashed Pascal
    • fibo.gg
That doesn't work because it can break existing code.  In existing code, if that case statement is in a loop the "break" should cause the exit of the loop

Fair point, that convinced me. No fallthrough then - first hit and stop.
Unleashed Pascal: async/await, parallel for, match, tuples, string interpolation, inline vars, autofree, no-RTTI & tons more. Star on GitHub ⭐

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 12820
  • Debugger - SynEdit - and more
    • wiki
Because in my scenario, True/False is not the only constant/result i can pass to such a Case-Construct
think about following aircode
Code: Pascal  [Select][+][-]
  1. Var i:Integer=5;
  2. ......
  3. Case 8 Of  //a literal constant "8"
  4. (i=5) : Writeln('i=5');  //This should call a compile-Error for the Label! "Integer-Result expected, got Boolean"
  5. i : Writeln('i='+IntToStr(8));   //Flipped around --> Label is an expression--> Will not execute --> "i=5" (from first line) which means 5<>8 --> False --> does not execute
  6. (i+3) : writeln('i+3='+IntToStr(8));  //No Fallthrough, but would execute, since it's the first hit
  7. End;

Yes, i'm complicating things now

If you already "complicate" things...

Lets pick up on this...

Code: Pascal  [Select][+][-]
  1. procedure foo;
  2. var A: integer;
  3.   function Bar: integer;
  4.   begin
  5.     inc(A);
  6.     result := A;
  7.   end;
  8. begin
  9.   A:=random(99);
  10.   case a of
  11.      Bar()+1: ;
  12.      Bar()+2  ;
  13.      Bar()+3  ;
  14.   end;
  15. end;

Each call to Bar modifies A which is being looked up.

So what should happen?

Should that be "for" loop style where the value (high/end value in for loop) is evaluated ONCE and the result used/chached, even if the value changes?
Or should it be "while" style, using in each step the current value of A?

Of course if the latter, what if it is
   "case MyFunc() of"
surely the MyFunc is only called once.

(again restricting to pur function / or maybe no functions ...)

440bx

  • Hero Member
  • *****
  • Posts: 6628
Fair point, that convinced me. No fallthrough then - first hit and stop.
There is nothing wrong with explicitly causing fall through by the addition of a keyword for that purpose.

For instance, forum user @Munair implemented fall through in case statements in his SharpBasic compiler using the keyword "fall".

If the language does not offer such a mechanism/keyword the "case" can be placed in an infinite loop that implements a FSM and is even more flexible and powerful than simple fall through.  An example is found at: https://forum.lazarus.freepascal.org/index.php/topic,42225.msg499898.html#msg499898
User @Munair contributed to the above thread and you can see his (semantic only) implementation of "fall" in SharpBasic.
FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 12820
  • Debugger - SynEdit - and more
    • wiki
Fair point, that convinced me. No fallthrough then - first hit and stop.
There is nothing wrong with explicitly causing fall through by the addition of a keyword for that purpose.

True, and not true as well... It depends on the overall situation.

In general (and there are existing counter examples, but that doesn't mean adding to that list is a good idea).... In general, you want to keep the "rules" of a feature simple.

That is, if you read a bit of code, you don't want to look for too many "exceptions from the rule".  Each optional behaviour means you must add extra care when reading the code, so you will not miss any bit that triggers such an optional behaviour. (fall-through on a keyword is optional).

This doesn't say, that there can't be optional behaviours, but balancing how many of them there should be is important.

Is it just and only fall-through and will always only be that?

What about the new "case"...?
What will fall-through do there?
- go to the next block that matches the condition (since more than one can).
- go to the next block (never mind the condition)
- will fall-through evaluate the condition of the next block? (triggering side effects in called functions)

The last may seem an odd question, because if it knows it enters the block, then why should it eval the condition?
But, then the code in the block may expect that the condition was executed, the code may rely on those side effects....


Note, I am not saying that any of that is right or wrong or should or shouldn't be done...

But, there are a lot of tiny details that need to be very exactly defined.
Don't define them now, or don't do it well, and the piper will have to be paid later.

And, while you single "fall-through" optional behaviour may be fine in terms of how much optional behaviour it adds (and if the code will be easily misread or not), it has a lot of decisions following it, and if they are introducing more optional behaviours => then that may change...


valdir.marcos

  • Hero Member
  • *****
  • Posts: 1285
FPC Unleashed

Upstream said no. FPC Unleashed says yes - inline variables, statement expressions, array equality, no-RTTI mode, and more.

FPC Unleashed is an experimental fork of FPC with additional language features that are not part of the official compiler and are unlikely to be included there. This is WIP and experimental. A hobby project. Compiles and runs, see for yourself.

Demo code below (screenshot attached). This example only shows the most recent feature - inline variables.

Code: Pascal  [Select][+][-]
  1. program app;
  2.  
  3. {$mode unleashed}
  4.  
  5. procedure main;
  6. begin
  7.   // *** INLINE VARIABLES ***
  8.  
  9.   var s := 'hi there';
  10.   var i := 42;
  11.   // or
  12.   var s2: string := 'hello';
  13.   var i2: integer := 66;
  14.  
  15.   writeln(s, ' | ', i);
  16.   writeln(s2, ' | ', i2);
  17.  
  18.   // *** INLINE VAR IN FOR LOOP ***
  19.  
  20.   for var j := 1 to 5 do writeln('j = ', j);
  21.  
  22.   // *** STATEMENT EXPRESSIONS + INLINE VARIABLES ***
  23.  
  24.   randomize;
  25.   var d := if random(20) > 10 then 'less than 10' else 'more than 10';
  26.   writeln('d = ', d);
  27.  
  28.   // *** FOR-IN + INLINE VAR ***
  29.  
  30.   var a := ['foo', 'bar', 'baz'];
  31.   for var z in a do writeln('a[] = ', z);
  32.  
  33.   readln;
  34. end;
  35.  
  36. begin
  37.   main;
  38. end.
  39.  



Want to try it?

The project is here: https://github.com/fpc-unleashed/freepascal

Install via fpcupdeluxe.

Open fpcup.ini and add to the top of [ALIASfpcURL]:
Code: INI  [Select][+][-]
  1. unleashed.git=https://github.com/fpc-unleashed/freepascal.git
  2.  

For Lazarus Unleashed (with autocomplete support for some of the new features), add to the top of [ALIASlazURL]:
Code: INI  [Select][+][-]
  1. unleashed.git=https://github.com/fpc-unleashed/lazarus.git
  2.  

Optionally enable docked IDE in Setup+, then click Install/update FPC+Lazarus.

Do you still remember the history of Maciej Izak [@hnb], from FPC core developer to creator of the NewPascal compiler fork, being kicked out of the FPC team in between?

NewPascal plans, Generics.Collections and FPC trunk
May 02, 2018
https://forum.lazarus.freepascal.org/index.php/topic,41128.0.html

Fibonacci

  • Hero Member
  • *****
  • Posts: 1125
  • Behold, I bring salvation - Unleashed Pascal
    • fibo.gg
Two options on the table.

Option A: case true/false of - first match stops

Code: Pascal  [Select][+][-]
  1. var i := 5;
  2.  
  3. case true of
  4.   i > 0: writeln('match 1'); // executes, stops here
  5.   i = 5: writeln('match 2'); // never reached
  6.   else   writeln('no match');
  7. end;

Pros: no new keyword, "case true of" signals "labels can be expressions" - user consciously writes a boolean literal. Existing code is unaffected - "case true of true: ... false: ..." still works exactly as before. No ambiguity, no breaking change.

Cons: only first match executes.

Option B: when - Multi-match by default, "break" stops early

Multi-match, "break" exits "when" only, does not affect enclosing loops. Since "when" is a new construct, this cannot break existing code - and there are other ways to exit a loop.

Code: Pascal  [Select][+][-]
  1. var i := 5;
  2.  
  3. when
  4.   i > 0: writeln('match 1'); // executes
  5.   i = 5: writeln('match 2'); // executes
  6.   i < 7: writeln('match 3'); // executes
  7. end;
  8.  
  9. when
  10.   i > 0:       writeln('match 1');             // executes
  11.   i = 5: begin writeln('match 2'); break; end; // executes, stops
  12.   i < 7:       writeln('match 3');             // never reached
  13. end;

Pros: semantically clean, no ambiguity.

Cons: new keyword - @PascalDragon commented on the Record Composition MR: "We try to avoid adding new keywords if an existing one can fulfill the purpose well enough."

Thoughts? Can add both - each has its use case: in "case true of" "break" exits the enclosing loop, in "when" it does not.



Do you still remember the history of Maciej Izak [@hnb], from FPC core developer to creator of the NewPascal compiler fork, being kicked out of the FPC team in between?

NewPascal plans, Generics.Collections and FPC trunk
May 02, 2018
https://forum.lazarus.freepascal.org/index.php/topic,41128.0.html

Nope, never seen this. Not planning to join core devs, even if invited, there are probably rules and expectations, and with unleashed I have full freedom. I prefer to keep it that way. It's a hobby project, mostly for myself, and maybe a bit for others.

I maintain a few sizeable projects in FPC so I have to use it anyway, unless I rewrite them in C, but with inline-vars and noRTTI there's no need. I'll sync with upstream from time to time, and if some MR shows up on GitLab I might pull it into unleashed. No grand mission here.
Unleashed Pascal: async/await, parallel for, match, tuples, string interpolation, inline vars, autofree, no-RTTI & tons more. Star on GitHub ⭐

440bx

  • Hero Member
  • *****
  • Posts: 6628
and what happens when "option B" is in a loop ? what will the "break" do ?  not exit the loop as expected ?



with unleashed I have full freedom. I prefer to keep it that way. It's a hobby project, mostly for myself, and maybe a bit for others.
Don't take this the wrong way but, you really need to study an introductory textbook on language and compiler design.  I suggest you start by reading "Per Brinch Hansen On Pascal Compilers" by far the best introductory text book I've seen on the subject.  You can find it a PDF of it in Hansotten's excellent Pascal centered web site.  Don't feel singled out, you're far from the only one who should carefully study that book.

HTH.

FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

 

TinyPortal © 2005-2018