Recent

Author Topic: [Solved] {$COPERATORS ON} ... for Strings?  (Read 551 times)

ArminLinder

  • Sr. Member
  • ****
  • Posts: 314
  • Keep it simple.
[Solved] {$COPERATORS ON} ... for Strings?
« on: March 26, 2023, 09:21:24 pm »
According to https://www.freepascal.org/docs-html/prog/progsu10.html Pasacal does support those sexy += operators.

I found that working for numbers, but not for Strings.

Code: Pascal  [Select][+][-]
  1. var
  2.   i:integer;
  3.  
  4. begin
  5.   i := 1;
  6.   i += 1;
  7.   if doAppend then
  8.     StatusBar1.SimpleText += (' ' + aMsg)
  9.   else
  10.     StatusBar1.SimpleText := aMsg;
  11. end;
  12.  

I get compile error "Error: Argument cannot be assigned to"

Did I miss something? I know one can define custom operators for various data types, can I do so for Strings too? And if yes, would that work for all the different types of Strings?

Thnx, Armin.
« Last Edit: March 26, 2023, 10:13:43 pm by Nimral »
Lazarus 3.3.2 on Windows 7,10,11, Debian 10.8 "Buster", macOS Catalina, macOS BigSur, VMWare Workstation 15, Raspberry Pi

korba812

  • Sr. Member
  • ****
  • Posts: 392
Re: {$COPERATORS ON} ... for Strings?
« Reply #1 on: March 26, 2023, 09:30:36 pm »
Operators work with string but you can't use them on properties.
This code works:
Code: Pascal  [Select][+][-]
  1. var
  2.   S: String;
  3. begin
  4.   S := 'abc';
  5.   S += '123';
  6. end;

ArminLinder

  • Sr. Member
  • ****
  • Posts: 314
  • Keep it simple.
Re: {$COPERATORS ON} ... for Strings?
« Reply #2 on: March 26, 2023, 10:13:20 pm »
Thanks for the info, this strange restriction when using a String property instead of a String variable never came to my mind ...

Armin.
Lazarus 3.3.2 on Windows 7,10,11, Debian 10.8 "Buster", macOS Catalina, macOS BigSur, VMWare Workstation 15, Raspberry Pi

Fred vS

  • Hero Member
  • *****
  • Posts: 3158
    • StrumPract is the musicians best friend
Re: [Solved] {$COPERATORS ON} ... for Strings?
« Reply #3 on: March 26, 2023, 10:46:36 pm »
According to https://www.freepascal.org/docs-html/prog/progsu10.html Pasacal does support those sexy += operators.

Thanks for the sexy tip  ;) !

Code: Pascal  [Select][+][-]
  1.  {$COPERATORS ON}
  2.  
  3. // The following operators are allowed:
  4. Var  
  5.   I : Integer;  
  6.  
  7. begin  
  8.   I:=1;  
  9.   I+=3; // Add 3 to I and assign the result to I;  
  10.   I-=2; // Subtract 2 from I and assign the result to I;  
  11.   I*=2; // Multiply I with 2 and assign the result to I;  
  12.   I/=2; // Divide I with 2 and assign the result to I;  
  13. end;
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
Re: {$COPERATORS ON} ... for Strings?
« Reply #4 on: March 27, 2023, 11:07:43 pm »
Thanks for the info, this strange restriction when using a String property instead of a String variable never came to my mind ...

This restriction applies to properties of any type, because a property might not be a direct field access, but instead a complex getter and setter. Same reason you can't take the address of a property or modify the fields of a property that returns a record.

 

TinyPortal © 2005-2018