Forum > FPC development
Difference between += and := -- Is this a bug in FPC?
EganSolo:
Before I get ahead of myself and post this as a bug, I thought I would ask here first.
Consider line 8 in the code fragment below: The code uses +=, whereas the commented assignment on line nine uses :=
When I run the code as is (with +=) and the boolean variable, EmptyStr on line seven is true, the debugger exits the method as if the code on lines eight and ten does not exist. But when I comment that line and uncomment line nine, then the code functions properly.
Am I simply misusing +=? I mean CurrentString is a variable passed to method where this code is implemented and MIF_EmtyCharStyleMarker is a constant declared in another unit.
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} --- Case WordHTMLTag of wht_Italics : CurrentString += MIF_ItalicMarker; wht_Bold : CurrentString += MIF_BoldMarker ; wht_span : If IsOpeningTag then begin EmptyStr := HtmlClass = ''; If EmptyStr then CurrentString += MIF_EmptyCharStyleMarker // CurrentString := CurrentString + MIF_EmptyCharStyleMarker else MIF_AddStartCharaTag(HTMLClass) end else If MIF_HasIllegalMarker(CurrentString) then CurrentString += MIF_EmptyCharStyleMarker else MIF_AddEndCharaTag; else Assert(False, 'we should not bere here!'); end;
Blaazen:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} --- CurrentString += MIF_EmptyCharStyleMarker CurrentString := CurrentString + MIF_EmptyCharStyleMarkerThese two lines are identical. There must be something wrong elsewhere out of pasted code.
munair:
You are not misusing += but it is C-style. I never use that style, but from the manual I find that you must use the compiler directive {$COPERATORS ON} to support it.
With a similar example, without the directive, I get a compile error "Variable identifier expected". Interestingly, WITH the directive I also get the same compile error. See the screenshot.
If I replace += with normal fp syntax, everything compiles just fine. My suggestion: stick to native fp syntax. Even though it may look more verbose, the resulting code is the same.
Example:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---unit upcstyle; {$mode objfpc}{$H+}{$COPERATORS ON} interface uses Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls; type { TForm1 } TForm1 = class(TForm) Label1: TLabel; procedure FormCreate(Sender: TObject); private { private declarations } public { public declarations } end; var Form1: TForm1; implementation {$R *.lfm} { TForm1 } procedure TForm1.FormCreate(Sender: TObject);var EmptyStr: boolean; HtmlClass: string;begin EmptyStr := (HtmlClass = ''); if EmptyStr then Label1.Caption += 'empty' //doesn't work //Label1.Caption := Label1.Caption + 'empty' //works else Label1.Caption := 'not empty';end; end.
Steps to reproduce: Start a new project. Add a label to the form. Replace the code in unit1 with the example code.
EDIT: I just learned that {$mode objfpc} already gives support to C style operators. It explains why the compiler keeps complaining. In the example above {$COPERATORS ON} doesn't make any difference.
engkin:
--- Quote from: EganSolo on October 18, 2017, 02:33:05 am ---Before I get ahead of myself and post this as a bug, I thought I would ask here first.
Consider line 8 in the code fragment below: The code uses +=, whereas the commented assignment on line nine uses :=
When I run the code as is (with +=) and the boolean variable, EmptyStr on line seven is true, the debugger exits the method as if the code on lines eight and ten does not exist. But when I comment that line and uncomment line nine, then the code functions properly.
Am I simply misusing +=? I mean CurrentString is a variable passed to method where this code is implemented and MIF_EmtyCharStyleMarker is a constant declared in another unit.
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} --- Case WordHTMLTag of wht_Italics : CurrentString += MIF_ItalicMarker; wht_Bold : CurrentString += MIF_BoldMarker ; wht_span : If IsOpeningTag then begin EmptyStr := HtmlClass = ''; If EmptyStr then CurrentString += MIF_EmptyCharStyleMarker // CurrentString := CurrentString + MIF_EmptyCharStyleMarker else MIF_AddStartCharaTag(HTMLClass) end else If MIF_HasIllegalMarker(CurrentString) then CurrentString += MIF_EmptyCharStyleMarker else MIF_AddEndCharaTag; else Assert(False, 'we should not bere here!'); end;
--- End quote ---
Your usage is correct.
The generated code is also correct.
There is a bug in the debug information, confirmed using -al in the compiler options
When += is used here is the generated code:
--- Quote ---.Ll28:
# [46] If EmptyStr then
cmpb $0,-20(%ebp)
je .Lj58
.Ll29:
# [49] else
pushl $0
movl 8(%ebp),%eax
movl (%eax),%edx
movl 8(%ebp),%eax
movl TC_$P$PROJECT1_$$_MIF_EMPTYCHARSTYLEMARKER,%ecx
call fpc_ansistr_concat
jmp .Lj32
--- End quote ---
While with CurrentString := CurrentString + MIF_EmptyCharStyleMarker
--- Quote ---.Ll28:
# [46] If EmptyStr then
cmpb $0,-20(%ebp)
je .Lj58
.Ll29:
# [48] CurrentString := CurrentString + MIF_EmptyCharStyleMarker
pushl $0
movl 8(%ebp),%eax
movl (%eax),%edx
movl 8(%ebp),%eax
movl TC_$P$PROJECT1_$$_MIF_EMPTYCHARSTYLEMARKER,%ecx
call fpc_ansistr_concat
jmp .Lj32
--- End quote ---
Notice that the assembly is identical. Just the debug information is wrong.
munair:
Does that mean that the directive {$COPERATORS ON} no longer makes a difference? My example suggests it is not just the debugger.
Navigation
[0] Message Index
[#] Next page