[fpc-pascal] User-defined operators and dummy parameters
Is it possible to pass a dummy parameter to a user-defined operator, with no expectation that it be referenced? The following test program compiles OK but fails at runtime with an error 217 on all platforms if the record is empty. It works if it contains a variant, but not reliably with other types. program test; {$mode objfpc}{$H+} typet1= array of integer; r_= record // x: variant end; var a1: t1; reduce: r_ = (); procedure print(const a: t1); var i: integer; begin for i := Low(a) to High(a) do Write(a[i], ' '); WriteLn end { print } ; operator + (const r: r_; const a: t1) s: variant; var i: integer; begin s := 0; for i := Low(a) to High(a) do s += a[i] end { + } ; begin // a1 := t1([1,2,3,4,5]); Doesn't work without tuple support SetLength(a1, 5); a1[0] := 1; a1[1] := 2; a1[2] := 3; a1[3] := 4; a1[4] := 5; WriteLn('a1:'); print(a1); WriteLn('+/ a1:'); WriteLn(reduce + a1); WriteLn end. This is of no particular importance, I'm just exploring capabilities. -- Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues] ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
[fpc-pascal] Re: User-defined operators and dummy parameters
works if I change the operator header to: operator + (const r: r_; const a: t1) s: integer; from gdb bt, the problem seems to be assignment from shortint to variant: #0 0x in ?? () #1 0x08057205 in SYSTEM_$$_assign$SHORTINT$$VARIANT () #2 0x0806bf94 in U_$SYSTEM_$$_OUTPUT () #3 0x0804816f in plus (R=..., A=0xb7ff7068) at x.pas:30 #4 0x080482ef in main () at x.pas:47 > Doesn't work without tuple support a1 := t1.create(1,2,3,4,5); -- View this message in context: http://free-pascal-general.1045716.n5.nabble.com/User-defined-operators-and-dummy-parameters-tp5716106p5716107.html Sent from the Free Pascal - General mailing list archive at Nabble.com. ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] Re: User-defined operators and dummy parameters
leledumbo wrote: works if I change the operator header to: operator + (const r: r_; const a: t1) s: integer; from gdb bt, the problem seems to be assignment from shortint to variant: #0 0x in ?? () #1 0x08057205 in SYSTEM_$$_assign$SHORTINT$$VARIANT () #2 0x0806bf94 in U_$SYSTEM_$$_OUTPUT () #3 0x0804816f in plus (R=..., A=0xb7ff7068) at x.pas:30 #4 0x080482ef in main () at x.pas:47 [Digs] Thanks, you're right :-) Also, if the array elements are variants then there's no error. So in other words, it might turn out to be safer to have separate operators returning integer, double and so on. I appreciate that I'm abusing the language here, but /should/ the original implicit conversion have worked? Doesn't work without tuple support a1 := t1.create(1,2,3,4,5); Noted. What happens in this case if a1 := t1.create(1,2,3,4,5); .. a1 := t1.create(6,7,8,9); Does there have to be an explicit destroy or similar? -- Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues] ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
[fpc-pascal] Re: User-defined operators and dummy parameters
> I appreciate that I'm abusing the language here, but /should/ the original implicit conversion have worked? I don't know, but IMO it should. Well, a variant should be able to accept any variant compatible type, right? > Does there have to be an explicit destroy or similar? Nope, dynamic arrays are reference counted. When you re-assign the variable with a new array, reference count of the previous array reaches 0 and thus automatically freed. -- View this message in context: http://free-pascal-general.1045716.n5.nabble.com/User-defined-operators-and-dummy-parameters-tp5716106p5716109.html Sent from the Free Pascal - General mailing list archive at Nabble.com. ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
[fpc-pascal] When does a reference counted object get freed automatically, and when not?
I've been exploring interfaces a bit, and fail to understand when automatic destruction of an object is precipitated by the compiler, and when not. Consider the lifetime of the two instances i1 and i2 in the following program. It seems that i2 (called in a method of i1) is freed automatically, but i1 is not. == code begin == program integerInterface; {$mode objfpc}{$H+} type ISortableInteger = interface ['{D38A748E-F889-4A2A-9E3C-5154D493061C}'] function Compare(anIntf: ISortableInteger): integer; procedure SetValue(aValue: int64); function GetValue: int64; property Value: int64 read GetValue write SetValue; end; { TSortableInteger } TSortableInteger = class(TInterfacedObject, ISortableInteger) strict private Fint: int64; procedure SetValue(aValue: int64); function GetValue: int64; public function Compare(anIntf: ISortableInteger): integer; property Value: int64 read GetValue write SetValue; end; { TSortableInteger } function TSortableInteger.Compare(anIntf: ISortableInteger): integer; begin if Fint > anIntf.Value then Result := 1 else if Fint = anIntf.Value then Result := 0 else Result := -1; end; function TSortableInteger.GetValue: int64; begin Result := Fint; end; procedure TSortableInteger.SetValue(aValue: int64); begin Fint := aValue; end; var i1, i2: TSortableInteger; begin i1 := TSortableInteger.Create; i1.Value := 21; i2 := TSortableInteger.Create; i2.Value := 22; WriteLn('i1 (21) compared to i2 (22) is ',i1.Compare(i2)); i1.Free; // why is this necessary with a reference counted interface? ReadLn; end. == code end == ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] When does a reference counted object get freed automatically, and when not?
In our previous episode, Howard Page-Clark said: > > var i1, i2: TSortableInteger; _INTERFACES_ are refcounted, not classes. And these are class references. Change it to ISortableInteger, remove the free, and it will work fine probably. ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal