Class operator +=



Class operator example
Pas2JS pascal source code
type TTest = class Field : String; procedure AppendString(str : String); class operator += String uses AppendString; end; type TSubTest = class(TTest) end; procedure TTest.AppendString(str : String); begin Field:=Field+str+','; end; { << TEST II >> } type TTest2 = class Field : String; procedure AppendString(str : String); procedure AppendInteger(i : Integer); class operator += String uses AppendString; class operator += Integer uses AppendInteger; end; procedure TTest2.AppendString(str : String); begin Field:=Field+'"'+str+'",'; end; procedure TTest2.AppendInteger(i : Integer); begin Field:=Field+IntToStr(i)+','; end; { << TEST III >> } type toa = array of const; type TTest3 = class Field : String; procedure AppendStrings(const str : toa); class operator += toa uses AppendStrings; end; procedure TTest3.AppendStrings(const str : array of const); var i : Integer; begin for i:=0 to High(str) do Field+=str[i]; end; { main.pas } var t = TTest.Create; st = TSubTest.Create; t2 = TTest2.Create; t3 = TTest3.Create; Begin t.AppendString('first'); WriteLn(t.Field); t += 'second'; WriteLn(t.Field); st.Field:='Hello '; st += 'World'; WriteLn(st.Field); { ### CONSOLE OUTPUTS ### first, first,second, Hello World, } { << TEST II >> } t2 += 1; WriteLn(t2.Field); t2 += 'second'; WriteLn(t2.Field); t2 += 3; WriteLn(t2.Field); { ### CONSOLE OUTPUTS ### 1, 1,"second", 1,"second",3, } { << TEST III >> } t3 += [1, 2]; WriteLn(t3.Field); t3 += ['a', 'b', 'c']; WriteLn(t3.Field); t3 += []; WriteLn(t3.Field); { ### CONSOLE OUTPUTS ### 12 12abc 12abc }