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

Fibonacci

  • Hero Member
  • *****
  • Posts: 1123
  • Behold, I bring salvation - Unleashed Pascal
    • fibo.gg
Feature request:

A clean syntax C-like ternary operator.

Example:
Code: Pascal  [Select][+][-]
  1.   x := iif a > b then a else b;
  2.   y := iif p <> nil then p.value else 0;
  3.  

This already exists in Unleashed - not as iif, but as regular if in expression position (Statement Expression):

Code: Pascal  [Select][+][-]
  1. var x := if a > b then a else b;
  2. var y := if p <> nil then p.value else 0;

Same semantics you're describing - lazy evaluation, mandatory else, type widening, valid anywhere an expression is valid. The context (RHS of assignment, function argument, etc.) disambiguates statement-if from expression-if.
Besides if, the same form works with case, match, and try - all four can sit in an expression position and produce a value.
Unleashed Pascal: async/await, parallel for, match, tuples, string interpolation, inline vars, autofree, no-RTTI & tons more. Star on GitHub ⭐

440bx

  • Hero Member
  • *****
  • Posts: 6627
This already exists in Unleashed - not as iif, but as regular if in expression position (Statement Expression):
One of these days I have to remember to stop getting older ;)
FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

Fibonacci

  • Hero Member
  • *****
  • Posts: 1123
  • Behold, I bring salvation - Unleashed Pascal
    • fibo.gg
Unleashed Pascal: async/await, parallel for, match, tuples, string interpolation, inline vars, autofree, no-RTTI & tons more. Star on GitHub ⭐

bytebites

  • Hero Member
  • *****
  • Posts: 800

Code: Pascal  [Select][+][-]
  1. if Obj is not TFoo then ...
  2. if x not in [Apple, Orange] then ...

Both forms are Delphi-compatible - Delphi has supported them for years.


This is claimed new feature in Delphi 13 less than year ago?
Harmless bot

Fibonacci

  • Hero Member
  • *****
  • Posts: 1123
  • Behold, I bring salvation - Unleashed Pascal
    • fibo.gg

Code: Pascal  [Select][+][-]
  1. if Obj is not TFoo then ...
  2. if x not in [Apple, Orange] then ...

Both forms are Delphi-compatible - Delphi has supported them for years.


This is claimed new feature in Delphi 13 less than year ago?

Whatever... Delphi has it, FPC doesn't, Unleashed added it. That's all that matters.
Unleashed Pascal: async/await, parallel for, match, tuples, string interpolation, inline vars, autofree, no-RTTI & tons more. Star on GitHub ⭐

Fibonacci

  • Hero Member
  • *****
  • Posts: 1123
  • Behold, I bring salvation - Unleashed Pascal
    • fibo.gg
A thought / question on generics syntax

Coding an app today, using some generics, and writing this felt absurd:

Code: Pascal  [Select][+][-]
  1. {$mode objfpc}
  2.  
  3. type
  4.   tmyclass = class
  5.     generic function myfunc<A, B>(arg1: A; arg2: B): boolean;
  6.   end;
  7.  
  8. generic function tmyclass.myfunc<A, B>(arg1: A; arg2: B): boolean;
  9. begin
  10. end;
  11.  
  12. var
  13.   myclass: tmyclass;
  14.   mybool: boolean;
  15. begin
  16.   myclass := tmyclass.Create;
  17.   mybool := myclass.specialize myfunc<Integer, String>(43, 'foo');
  18. end.

Unreadable, awkward to write, hard to remember. Class name first, then specialize? I thought Unleashed was broken at first - checked trunk {$mode objfpc} and no, same behavior.

In Unleashed there is a modeswitch to sidestep this:

Code: Pascal  [Select][+][-]
  1. {$mode objfpc}
  2. {$modeswitch implicitgenerics}
  3.  
  4. type
  5.   tmyclass = class
  6.     function myfunc<A, B>(arg1: A; arg2: B): boolean;
  7.   end;
  8.  
  9. function tmyclass.myfunc<A, B>(arg1: A; arg2: B): boolean;
  10. begin
  11. end;
  12.  
  13. var
  14.   myclass: tmyclass;
  15.   mybool: boolean;
  16. begin
  17.   myclass := tmyclass.Create;
  18.   mybool := myclass.myfunc<Integer, String>(43, 'foo');
  19. end.

Used to not even be a modeswitch - it was just "if mode = Delphi then allowed". Now it's {$modeswitch implicitgenerics}, works in any mode.

10x better, right?

So three paths:
  • Enable implicitgenerics by default in {$mode unleashed}.
  • Spend a day supporting both forms in parallel - generic + specialize keywords AND the keyword-less form, both work, programmer chooses. Some work in FPC and on the IDE side, but doable.
  • Leave it as is.
Looking specifically for input from people actually using Unleashed - other opinions aren't really moving the needle on this one. The standard rule of "nothing should change, leave Pascal alone" has been broken many times in this fork already and will be broken many more.



There's another modeswitch I could theoretically enable, but I'm leaning against - unless someone changes my mind.

It's implicitfunctionspecialization:

Code: Pascal  [Select][+][-]
  1. {$mode objfpc}
  2. {$modeswitch implicitgenerics}
  3. {$modeswitch implicitfunctionspecialization}
  4.  
  5. type
  6.   tmyclass = class
  7.     function myfunc<A, B>(arg1: A; arg2: B): boolean;
  8.   end;
  9.  
  10. function tmyclass.myfunc<A, B>(arg1: A; arg2: B): boolean;
  11. begin
  12.   writeln(sizeof(arg1));
  13.   writeln(sizeof(arg2));
  14. end;
  15.  
  16. var
  17.   myclass: tmyclass;
  18.   mybool: boolean;
  19. begin
  20.   myclass := tmyclass.Create;
  21.   mybool := myclass.myfunc(300, 'f');
  22.   readln;
  23. end.

The problem: 300 infers as WORD, and 'f' infers as CHAR. The modeswitch would need to be tweaked so the defaults are String + Integer (like inferred inline-vars do). I could make a new modeswitch that does this and ship it in mode Unleashed.

But what else could "go wrong" beyond numbers and strings? Someone stop me before I implement this, because I know it would be a bad move ;)
Unleashed Pascal: async/await, parallel for, match, tuples, string interpolation, inline vars, autofree, no-RTTI & tons more. Star on GitHub ⭐

Curt Carpenter

  • Hero Member
  • *****
  • Posts: 769
I took a course in French history some years ago, and was fascinated to learn that for most of the 18th century, people in France -- sometimes in adjacent villages -- found it difficult to communicate with one another due to their differing dialects of French.  It took a revolution and the Napoleonic Wars to unify the language into what we know as French today.  Many of the world's great writers (Hugo, Dumas, Voltaire,...) would have been read only by a select few if the unification had never occurred.

I offer this as a cautionary tale for enthusiastic inventors of Pascal dialects.

H₂SO₄

  • Hero Member
  • *****
  • Posts: 517
The problem: 300 infers as WORD, and 'f' infers as CHAR. The modeswitch would need to be tweaked so the defaults are String + Integer (like inferred inline-vars do). I could make a new modeswitch that does this and ship it in mode Unleashed.

But what else could "go wrong" beyond numbers and strings? Someone stop me before I implement this, because I know it would be a bad move ;)

Overly aggressive type inference often leads to surprises. I think that unless there's a single, obvious specialization, the compiler should refuse and spit out a message similiar to Rust's "type annotations needed" error.

Since the concrete type of most literals is determined either by content or context (e.g.  0.5 → Single,  0.1 → Extended,  'foo' → AnsiString  or  WideString  or  UnicodeString  etc., array types are already a mess), I'd ignore them entirely for the purpose of type inference (unless the literal's type is made unambiguous by insertion of a cast; other languages have suffixes like  100i32  or  3.14d,  but alas).

Code: Pascal  [Select][+][-]
  1. function Sum<T>(const Values: array of T): T;
  2. var
  3.   Item: T;
  4. begin
  5.   Result := Default(T);
  6.   for Item in Values do
  7.     Result := Result + Item;
  8. end;
  9.  
  10. procedure Foo();
  11. begin
  12.   WriteLn(Sum([1, Integer.MaxValue])); // Integer? Cardinal? Int64? QWord?
  13.   WriteLn(Sum([0.5, 0.25, 0.1]));      // Single? Double? Extended?
  14. end;

Fibonacci

  • Hero Member
  • *****
  • Posts: 1123
  • Behold, I bring salvation - Unleashed Pascal
    • fibo.gg
Thanks for the input - valuable point, appreciate it.

Forget type inference and implicitfunctionspecialization for now - that was the side question. The main one was above:

1) implicitgenerics on by default in {$mode unleashed}
2) Make implicitgenerics + keywords work together

Which would you choose?
Unleashed Pascal: async/await, parallel for, match, tuples, string interpolation, inline vars, autofree, no-RTTI & tons more. Star on GitHub ⭐

H₂SO₄

  • Hero Member
  • *****
  • Posts: 517
1) implicitgenerics on by default in {$mode unleashed}
2) Make implicitgenerics + keywords work together

Which would you choose?

I'd go for #1.

I don't think the  generic  keyword is that bad, but  specialize  deserves a special place in hell - it's incredibly verbose (especially for a keyword that's likely to be used all over the place) and its placement in method calls is bewildering (following a very simple rule, but unintuitive nonetheless).

flowCRANE

  • Hero Member
  • *****
  • Posts: 1010
Regarding unions (variant records) and bit fields, I agree. Pascal has limitations in this regard. A solution to this issue would be useful. Although not necessarily by copying C syntax verbatim.

My proposal for a new union syntax is not copied from the C language. It is the result of careful consideration of what a union syntax should look like—one that aligns as closely as possible with Pascal syntax, is simple, concise, readable, unambiguous, and C-ABI-compatible. I believe that my proposal, which was ultimately implemented, meets all of the above requirements.

Quote
You must be joking. The syntax of C and C++ has also remained unchanged for decades. Is it also clunky and deviates from the norm? If that's your opinion, I completely agree with you. And should the syntax of these languages ​​be improved? If that's what you think, then I agree on that point too. Unfortunately, no one intends to correct it. On the contrary, C and C++ enthusiasts insist it's great (which is a lie).

Among the dozen or so most popular general-purpose programming languages, there isn’t a single one whose array declaration syntax requires specifying indices instead of the number of elements. None of these languages has syntax as verbose as Pascal’s, with the sole exceptions being niche languages such as Ada, Fortran, and VBA. Furthermore, all modern programming languages allow you to declare arrays by specifying the number of elements, with the sole exception of niche languages such as Pascal and Ada.

While my proposal is indeed inspired by other languages, its purpose is not to remove the ability to specify ranges from the language, but rather to shorten overly long syntax and add the ability to specify the number of elements (which is more important in 99.9% of cases).



I don't really have time to answer the rest of your questions. If you want to know what array declarations look like in other languages, just do some research (I recommend asking Google Gemini, for example, to provide a comprehensive analysis and comparison of language syntax). It’s no secret that Pascal is an exception when it comes to the pointlessly verbose nature of its syntax. However, the fact that Pascal’s current syntax is unambiguous does not mean that a shorter version (similar to those in other languages) would cease to be readable, unambiguous, and easy to understand.
Lazarus 4.8 with FPC 3.2.2, Windows 11 — all 64-bit

Working solo on a top-down retro-style action/adventure game (pixel art), programming the engine from scratch, using Free Pascal and SDL3.

Fibonacci

  • Hero Member
  • *****
  • Posts: 1123
  • Behold, I bring salvation - Unleashed Pascal
    • fibo.gg
While my proposal is indeed inspired by other languages, its purpose is not to remove the ability to specify ranges from the language, but rather to shorten overly long syntax and add the ability to specify the number of elements (which is more important in 99.9% of cases).

I want to implement this. Any concrete syntax in mind?

Initial sketch:

Code: Pascal  [Select][+][-]
  1. string[10];             // existing - ShortString with max 10 chars
  2. array[0..9] of string;  // current - explicit range
  3. array[10] of string;    // new - count form, equivalent to array[0..9]
Unleashed Pascal: async/await, parallel for, match, tuples, string interpolation, inline vars, autofree, no-RTTI & tons more. Star on GitHub ⭐

Zvoni

  • Hero Member
  • *****
  • Posts: 3595
Line 3 looks good, syntax wise.
basically the same as i'm used from vb6/vba

In the end it's just parsing with sanity-checks.
Remember: fpc supports "type" as index in array-declaration
e.g.
Code: Pascal  [Select][+][-]
  1. Type MyEnum=(meOne, meTwo, meThree);
  2.  
  3. Var
  4.   a:array[MyEnum] Of String; //It's an "implicit Range"
  5.   b:array[Byte] Of Integer;  //It's still an "implicit Range"

You would have to parse for "literal" Integers.

and don't forget multiple dimensions array
Code: Pascal  [Select][+][-]
  1. Var
  2.   a:Array[MyEnum,11..20,100, Byte] Of TSomeType;

..and yes...i'm aware that above is "extreme"...  :P :P :P
« Last Edit: May 27, 2026, 01:54:32 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

flowCRANE

  • Hero Member
  • *****
  • Posts: 1010
@Fibonacci: when it comes to user-friendly generic syntax, it would be ideal to be able to omit the generic and specialize keywords, as well as avoid having to declare a separate data type for generic class constructors. In other words, it would be great if we could use the same syntax for generics by default as we do in the {$MODE DELPHI} mode.

Below does not compile in OBJFPC mode, compile in DELPHI mode and should in the dialect you develop:

Code: Pascal  [Select][+][-]
  1. type
  2.   TFoo<T> = class
  3.   end;
  4.  
  5. var
  6.   Foo: TFoo<Integer>;
  7. begin
  8.   Foo := TFoo<Integer>.Create();
  9. end.

Code: Pascal  [Select][+][-]
  1. type
  2.   TFoo<T> = class
  3.   end;
  4.  
  5. begin
  6.   with TFoo<Char>.Create() do
  7.     Free();
  8. end.

As for the implicit specialization of generic function calls, I don’t have a strong opinion. On the one hand, the shorter syntax requires less typing, but the compiler will have more work determining how to handle the given parameters based on their types. Here, the problem is identical to that with overloaded functions and can be just as annoying for the user as compilation errors caused by the compiler’s inability to determine which of the overloaded function versions to use. On the other hand, the requirement to specify data types in angle brackets requires writing a bit more code, but it provides clarity and makes the compiler’s job easier, since it doesn’t have to determine the types on its own (through inference). Here, I would lean toward explicitly specifying types, but of course without the specialize keyword.

Additionally, when it comes to generic functions, it would be highly desirable to require syntactic consistency between the declaration and definition of such functions. In other words, the header of a generic function should be identical in both its declaration (the module’s interface section) and its definition (the implementation section). The only thing to watch out for is forward declarations of such functions, because the language allows for a shorthand notation for forward function definitions, i.e., without specifying the list of arguments (which is absurd).

In fact, there are more of these absurd syntactic monstrosities in Pascal, because you can also omit empty parentheses in parameterless routines (declarations, definitions, calls), as well as the semicolon after the last statement in a given code block. It would be good if the dialect you're working on required the mandatory use of empty parentheses and semicolons, so as to eliminate those syntactic nonsenses. You can consider this as another suggestion for consideration and implementation.



I want to implement this. Any concrete syntax in mind?

In previous posts, I provided examples of syntax for both fixed-size arrays and dynamic arrays. This syntax is consistent with that of modern Pascal but is not as verbose, resembling the syntax used in all of today’s most popular programming languages. I will provide the examples again:

Code: Pascal  [Select][+][-]
  1. var
  2.   Foo: Integer[8];              // 1D, fixed size, 8 elements, implicit indexes 0..7
  3.   Foo: Integer[0 .. 7];         // 1D, fixed size, 8 elements, explicit indexes
  4. var
  5.   Foo: Integer[8, 8];           // 2D, fixed size, 8x8 elements, implicit indexes 0..7,0..7
  6.   Foo: Integer[8, 1 .. 8];      // 2D, fixed size, 8x8 elements, explicit indexes 0..7,1..8
  7.   Foo: Integer[1 .. 8, 1 .. 8]; // 2D, fixed size, 8x8 elements, explicit indexes 1..8,1..8
  8. var
  9.   Foo: Integer[];               // 1D, dynamic size,                     instead of "Foo: array of Integer"
  10.   Foo: Integer[,];              // 2D, dynamic size for both dimensions, instead of "Foo: array of array of Integer"
  11.   Foo: Integer[8,];             // 2D, dynamic size for 2nd dimension,   instead of "Foo: array [0 .. 7] of array of Integer"
  12.  
  13.   procedure Foo(ABar: const[]); // instead of "ABar: array of const"
« Last Edit: May 27, 2026, 02:06:44 pm by flowCRANE »
Lazarus 4.8 with FPC 3.2.2, Windows 11 — all 64-bit

Working solo on a top-down retro-style action/adventure game (pixel art), programming the engine from scratch, using Free Pascal and SDL3.

Fibonacci

  • Hero Member
  • *****
  • Posts: 1123
  • Behold, I bring salvation - Unleashed Pascal
    • fibo.gg
@Fibonacci: when it comes to user-friendly generic syntax, it would be ideal to be able to omit the generic and specialize keywords, as well as avoid having to declare a separate data type for generic class constructors. In other words, it would be great if we could use the same syntax for generics by default as we do in the {$MODE DELPHI} mode.

Already done. implicitgenerics is on by default in {$mode unleashed}. If someone doesn't like it, they can opt out with {$modeswitch implicitgenerics-}.
Unleashed Pascal: async/await, parallel for, match, tuples, string interpolation, inline vars, autofree, no-RTTI & tons more. Star on GitHub ⭐

 

TinyPortal © 2005-2018