Recent

Author Topic: language improvement suggestions  (Read 3088 times)

amber

  • Newbie
  • Posts: 1
language improvement suggestions
« on: November 22, 2019, 05:16:05 pm »
Hello! Sometimes I have to use "If" or "case" comparing the same property on different objects. Now it may look cumbersome, for example:

If (obj1.Caption=...) and (obj2.Caption=...) and (obj3....)and...

In Pascal there is a "set of", but it does not work for multiple change or comparison. I would like to have such a design:

If [Obj1, Obj2, Obj3...].Caption=... then... - here the Caption property of each object is compared with the specified value. If they are all equal, then the condition is satisfied. Each object in the set must have a "Caption" property of the same type.

More example:
[Obj1, Obj2, Obj3...].Caption:= 'Yes' - here the "Caption" property of all objects is changed.

Also, it would be great to be able to call functions and procedures, for example:

[Form1, Form2, Form3].Show -- this replaces:
Form1.Show; Form2.Show; Form3.Show

"set of case" would also be useful in "case of", for example:

Case [Form1, Form2, Form3].Top of
10: a:=10;
15..20: a:=20;
end;

Now it looks like this:

Case Form1.Top of
10: a:=10;
15..20: a:=20;
end;

Case Form2.Top of
10: a:=10;
15..20: a:=20;
end;

Case Form3.Top of
10: a:=10;
15..20: a:=20;
end;

This would be useful for variables of the same type, for example:

[a, b, c]:=10; -- this is the same as now:

a:=10; b:=10; c:=10;

The quadrad brackets [] should read as "Union action".

For the "union action" to work, need two or more objects or vars. Also, there should be no characters before "[" (variable names, for example), otherwise it will be considered as a reference to a variable or property array. It may make sense to introduce other restrictions to use this.


Thank you, I hope my suggestions will be useful for the development of the programming language.
« Last Edit: November 22, 2019, 05:27:13 pm by amber »

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 5788
    • wiki
Re: language improvement suggestions
« Reply #1 on: November 22, 2019, 05:43:52 pm »
Not sure if this is in FPC 3.0.4 or trunk only....

But you can use an array.

Code: Pascal  [Select]
  1. type TFormArray = array of TForm;
  2. var a: TForm;
  3.  
  4.   for a in TFormArray.create(Form1, Form2, Form3) do begin
  5.     a.Caption := 'foo';
  6.   end;
  7.  

korba812

  • Full Member
  • ***
  • Posts: 109
Re: language improvement suggestions
« Reply #2 on: November 22, 2019, 06:00:22 pm »
Yes, arrays in trunk are very powerful.
Code: Pascal  [Select]
  1. program Project1;
  2.  
  3. {$modeswitch TypeHelpers}
  4.  
  5. uses
  6.   Classes;
  7.  
  8. type
  9.   TPIntArray = array of PInteger;
  10.   TCompArray = array of TComponent;
  11.  
  12.   { TPIntArrayHelper }
  13.  
  14.   TPIntArrayHelper = type helper for TPIntArray
  15.     procedure Assign(AValue: Integer);
  16.   end;
  17.  
  18. procedure AssignVal(Ar: TPIntArray; Value: Integer);
  19. var
  20.   I: PInteger;
  21. begin
  22.   for I in Ar do
  23.     I^ := Value;
  24. end;
  25.  
  26. function AllTagEqual(Ar: TCompArray; AVal: Integer): Boolean;
  27. var
  28.   C: TComponent;
  29. begin
  30.   for C in Ar do
  31.     if C.Tag <> AVal then
  32.       Exit(False);
  33.   Result := True;
  34. end;
  35.  
  36. var
  37.   a,b,c: Integer;
  38.   c1,c2,c3: TComponent;
  39.   cm: TComponent;
  40.  
  41. { TPIntArrayHelper }
  42.  
  43. procedure TPIntArrayHelper.Assign(AValue: Integer);
  44. var
  45.   I: PInteger;
  46. begin
  47.   for I in Self do
  48.     I^ := AValue;
  49. end;
  50.  
  51. begin
  52.   AssignVal([@a, @b, @c], 10);
  53.   writeln(a, ' ', b, ' ', c);
  54.   TPIntArray([@a, @b, @c]).Assign(20);
  55.   writeln(a, ' ', b, ' ', c);
  56.  
  57.   c1 := TComponent.Create(nil);
  58.   c1.Tag := 1;
  59.   c2 := TComponent.Create(nil);
  60.   c2.Tag := 2;
  61.   c3 := TComponent.Create(nil);
  62.   c3.Tag := 3;
  63.   for cm in [c1, c2, c3] do
  64.     case cm.Tag of
  65.       1: writeln('1');
  66.       2: WriteLn('2');
  67.       3: WriteLn('3');
  68.     end;
  69.   WriteLn(AllTagEqual([c1, c2, c3], 10));
  70.   for cm in [c1, c2, c3] do
  71.     cm.Free;
  72.  
  73.   readln;
  74. end.
  75.  

PascalDragon

  • Hero Member
  • *****
  • Posts: 716
  • Compiler Developer
Re: language improvement suggestions
« Reply #3 on: November 23, 2019, 12:19:33 pm »
Yes, arrays in trunk are very powerful.
Should be supported by the upcoming 3.2 as well. :)

Thank you, I hope my suggestions will be useful for the development of the programming language.
As you can see using already implemented (if not yet released) language features you can achieve something similar without the need to extend the language in some strange way.

simone

  • Sr. Member
  • ****
  • Posts: 260
Re: language improvement suggestions
« Reply #4 on: November 23, 2019, 01:10:47 pm »
Not sure if this is in FPC 3.0.4 or trunk only....

But you can use an array.

Code: Pascal  [Select]
  1. type TFormArray = array of TForm;
  2. var a: TForm;
  3.  
  4.   for a in TFormArray.create(Form1, Form2, Form3) do begin
  5.     a.Caption := 'foo';
  6.   end;
  7.  

FPC 3.0.4 already supports the feature used in the above code snipped.
« Last Edit: November 23, 2019, 01:30:10 pm by simone »

Zvoni

  • Sr. Member
  • ****
  • Posts: 299
Re: language improvement suggestions
« Reply #5 on: November 23, 2019, 03:51:06 pm »
To stay with the OP's original idea:
I did like a feature from VB6/VBA

Code: C  [Select]
  1. Select Case True
  2. Case Form1.Caption='Cap 1'
  3.   DoSomething1
  4. Case Form1.Caption='Cap 2'
  5.   DoSomething2
  6. Case Form2.Top=10
  7.   MoveSomething1
  8. Case Else
  9.    NukeWhiteHouse
  10. End Select
  11.  
One System to rule them all, One IDE to find them,
One Code to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
People call me crazy, because i'm jumping out of perfectly fine aircrafts

Thaddy

  • Hero Member
  • *****
  • Posts: 9285
Re: language improvement suggestions
« Reply #6 on: November 23, 2019, 04:09:09 pm »
seems a bit overbloated and verbose:
Code: Pascal  [Select]
  1. Case Form1.Caption of
  2. 'Cap 1':DoSomething1;
  3. 'Cap 2':DoSomething2;
  4. Else
  5.    NukeTrumpTower; // do not touch Whitehouse
  6. End;
also related to equus asinus.

Zvoni

  • Sr. Member
  • ****
  • Posts: 299
Re: language improvement suggestions
« Reply #7 on: November 23, 2019, 08:06:24 pm »
Thaddy,  :-*
i was more thinking along the lines that i wouldn't mind a feature like
Code: [Select]
Case True of
cond1>1:DoSomething;
cond2=10:DoSomethingElse;
Else Kaboom;
end;

But i can live without it...  :P 8)
One System to rule them all, One IDE to find them,
One Code to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
People call me crazy, because i'm jumping out of perfectly fine aircrafts

korba812

  • Full Member
  • ***
  • Posts: 109
Re: language improvement suggestions
« Reply #8 on: November 23, 2019, 08:35:20 pm »
i was more thinking along the lines that i wouldn't mind a feature like
 live without it...  :P 8)
what's wrong with "if..then..else" statement?
Code: Pascal  [Select]
  1. if cond1>1 then
  2.   DoSomething
  3. else if cond2=10 then
  4.   DoSomethingElse
  5. else
  6.   Kaboom;
  7.  

440bx

  • Hero Member
  • *****
  • Posts: 1272
Re: language improvement suggestions
« Reply #9 on: November 23, 2019, 09:26:26 pm »
what's wrong with "if..then..else" statement?
The conditional expressions are cluttered with "noise" in the form of "if", "then", "else" which make the logical conditions not as evident.  Additionally, the "noise" increases significantly with the number of conditions per case.

Code: C  [Select]
  1. Select Case True
  2.   Case Form1.Caption='Cap 1'
  3.      DoSomething1
  4.   Case Form1.Caption='Cap 2'
  5.      DoSomething2
  6.   Case Form2.Top=10
  7.      MoveSomething1
  8.   Case Else
  9.      NukeWhiteHouse
  10. End Select
  11.  
Is much cleaner and easier to read.
using FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

korba812

  • Full Member
  • ***
  • Posts: 109
Re: language improvement suggestions
« Reply #10 on: November 23, 2019, 11:07:52 pm »
The conditional expressions are cluttered with "noise" in the form of "if", "then", "else" which make the logical conditions not as evident.  Additionally, the "noise" increases significantly with the number of conditions per case.
Introduction of a new statement along with new keywords will not improve the readability of the program at all. Especially that it doesn't add anything new to the language but only replaces the existing syntax. For me this is unnecessary noise