* * *

Author Topic: Dynamic array extensions ?  (Read 4465 times)

circular

  • Hero Member
  • *****
  • Posts: 2726
    • Personal webpage
Re: Dynamic array extensions ?
« Reply #15 on: June 14, 2018, 10:18:59 pm »
I read the FP thread. Seems there has been a debate over there.

If I understand correctly:
Code: Delphi  [Select]
  1. var a: array of integer;
  2. begin
  3.   a := [1,2,3];
  4.   a += 1;          // will give a = [1,2,3,1] instead a = [2,3,4]
  5.   a := [1,2,3];
  6.   a += [4,5,6];  // will give a = [1,2,3,4,5,6] instead of a = [5,7,9]
  7. end;

Correct?
« Last Edit: June 15, 2018, 10:44:25 pm by circular »
Conscience is the debugger of the mind

Nitorami

  • Sr. Member
  • ****
  • Posts: 330
Re: Dynamic array extensions ?
« Reply #16 on: June 15, 2018, 10:23:37 am »
Yes, that seems to be the plan - the "+" operator acting as concatenation, defined as intrinsic in the language so it can't be overloaded anymore, e.g. to do numeric addition instead.





schuler

  • Jr. Member
  • **
  • Posts: 82
Re: Dynamic array extensions ?
« Reply #17 on: June 15, 2018, 10:43:21 am »
Nitorami,
Sorry. I evidently didn't understand your post first time I read.

I do have concerns with "+". I think that everyone that uses Free Pascal for math will have concerns.

In my case, it won't brake any code although I'm looking for another symbol in the keyboard that could be used for concat. What about & for concatenation?

BTW, is this concatenation or union? If it is union, I might suggest || instead.
« Last Edit: June 15, 2018, 10:45:21 am by schuler »

Nitorami

  • Sr. Member
  • ****
  • Posts: 330
Re: Dynamic array extensions ?
« Reply #18 on: June 15, 2018, 11:16:48 am »
Code: Pascal  [Select]
  1. BTW, is this concatenation or union? If it is union, I might suggest || instead.

They meant concatenation, probably for similarity between ansistrings and dynamic arrays. Other operators have been proposed, read more in the mailing list discussion http://free-pascal-general.1045716.n5.nabble.com/Feature-announcement-Dynamic-array-extensions-td5731410.html

But I don't understand why we need an intrinsic operator at all. I would leave it to the programmer to define one, whether it is for concatenation, addition, or whatever else is required.

circular

  • Hero Member
  • *****
  • Posts: 2726
    • Personal webpage
Re: Dynamic array extensions ?
« Reply #19 on: June 15, 2018, 10:46:44 pm »
@schuler

Agreed that "&" would be nice, like in VB. It could be also used in strings:
Code: Delphi  [Select]
  1. var s: string; i: integer;
  2. begin
  3.   i := 36;
  4.   s := 'hello number ' & i; // would yield 'hello number 36'
  5. end;
 
Conscience is the debugger of the mind

Bart

  • Hero Member
  • *****
  • Posts: 3012
    • Bart en Mariska's Webstek
Re: Dynamic array extensions ?
« Reply #20 on: June 16, 2018, 12:59:33 pm »
OMG. This is Pascal, it is strongtyped.
If you want such syntax, please use Basic.

Bart

circular

  • Hero Member
  • *****
  • Posts: 2726
    • Personal webpage
Re: Dynamic array extensions ?
« Reply #21 on: June 16, 2018, 03:47:07 pm »
 :D
Conscience is the debugger of the mind

lainz

  • Hero Member
  • *****
  • Posts: 2769
  • I'm coding :)
    • Lainz
Re: Dynamic array extensions ?
« Reply #22 on: June 16, 2018, 04:03:33 pm »
You can just add the + to convert automatically to string.

Code: Pascal  [Select]
  1. Operator + (s : string; i : integer) z : string;
  2. begin
  3.   z := s + IntToStr(i);
  4. end;
  5.  
  6. {$R *.lfm}
  7.  
  8. { TForm1 }
  9.  
  10. procedure TForm1.Button1Click(Sender: TObject);
  11. var
  12.   s: string;
  13.   i: integer;
  14. begin
  15.   s := 'Hello world ';
  16.   i := 10;
  17.   s := s + i; // 'Hello world 10'
  18.   ShowMessage(s);
  19. end;  

circular

  • Hero Member
  • *****
  • Posts: 2726
    • Personal webpage
Re: Dynamic array extensions ?
« Reply #23 on: June 16, 2018, 07:02:42 pm »
Indeed that's cool  :)
Conscience is the debugger of the mind

Abelisto

  • Jr. Member
  • **
  • Posts: 69
Re: Dynamic array extensions ?
« Reply #24 on: June 17, 2018, 09:26:48 pm »
@circular, @lainz

Code: Pascal  [Select]
  1. WriteStr(s, 'Hello number ', 10, '!');
OS: Linux Mint + MATE, Compiler: FPC trunk (yes, I am risky!), IDE: Lazarus trunk

circular

  • Hero Member
  • *****
  • Posts: 2726
    • Personal webpage
Re: Dynamic array extensions ?
« Reply #25 on: June 18, 2018, 05:10:39 pm »
@abelisto
Thanks I did not know this one
Conscience is the debugger of the mind

lainz

  • Hero Member
  • *****
  • Posts: 2769
  • I'm coding :)
    • Lainz
Re: Dynamic array extensions ?
« Reply #26 on: June 18, 2018, 07:05:03 pm »
@circular, @lainz

Code: Pascal  [Select]
  1. WriteStr(s, 'Hello number ', 10, '!');

Looks really good =)

I can format float as well with it.
Code: Pascal  [Select]
  1. WriteStr(s, 'hello cañón 人物 ', 10.5678:2:2, '!');

PascalDragon

  • Full Member
  • ***
  • Posts: 130
  • Compiler Developer
Re: Dynamic array extensions ?
« Reply #27 on: June 20, 2018, 10:30:14 pm »
I would like to draw your attention to a new feature announced on the FPC mailing list: Dynamic array extensions. In short:

- support for array constructors using "[...]" syntax
- support for Insert(), Delete() and Concat()
- support for "+" operator
- support for dynamic array constants (and variable initializations)

Sounds ok, however: The "+" operator is effectively an alias for concat(), and it will be an intrinsic, which means that existing "+" operator overloads for dynamic arrays will no longer compile. Intrinsic operators cannot be overloaded, hence it will no longer be possible to custom define the "+" operator to do a mathematical elementwise addition of numerical vectors/matrices.

With the "+" operator hijacked, all other obvious mathematical operators i.e. -, *, / for numeric vectors are effectively obliterated. We would either have to revert to procedural style as in turbo pascal days, such as in function add (V1, V2: TVector): TVector; or declare extra container classes for numeric vectors / matrices, to which mathematical operators can be applied.

What is your opinion on that  ?

Addendum to that: the "+" operator is now connected to a "ArrayOperators" modeswitch.

If the modeswitch is active then the "+" operator overload is not possible and the compiler will warn if such an overload is in scope. If the modeswitch is not set, then everything is as before.

The modeswitch is enabled by default in Delphi modes, because that's why the feature was added. To disable the modeswitch there use this:

Code: Pascal  [Select]
  1. {$mode Delphi}
  2. {$modeswitch ArrayOperators-} // Note the "-"

Thaddy

  • Hero Member
  • *****
  • Posts: 6342
Re: Dynamic array extensions ?
« Reply #28 on: June 20, 2018, 10:32:10 pm »
Neat solution!
I am enjoining wine, not whine....

Nitorami

  • Sr. Member
  • ****
  • Posts: 330
Re: Dynamic array extensions ?
« Reply #29 on: June 21, 2018, 09:45:49 pm »
Quote
Addendum to that: the "+" operator is now connected to a "ArrayOperators" modeswitch.

Thank you. Guess that is an acceptable solution.

Code: Pascal  [Select]
  1. {$modeswitch ArrayOperators-} // Note the "-"
Why is it "-" in this case rather than "OFF" ?

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus