Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2013-08

User-defined operators and dummy parameters

6 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Mark Morgan Lloyd

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+}
type    t1= 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]

Reply by email

leledumbo
to Mark Morgan Lloyd

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 0x00000000 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 .

Reply by email

Mark Morgan Lloyd
to leledumbo
leledumbo wrote 11 lines
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 0x00000000 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]

Reply by email

leledumbo
to Mark Morgan Lloyd
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 .

Reply by email

Sven Barth
to Mark Morgan Lloyd: Is it possible to pass a dummy parameter to a user-defined operator, with no expectation…
Mark Morgan Lloyd wrote 6 lines
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.

It seems that the compiler does not detect here that the variants unit is needed and thus does not implicitely add it. Please open a bug report with your code as an example. As a workaround simply add the unit "variants" to the unit where your operator is defined.

Btw: You don't need the "s: Variant" for operators in mode Delphi or mode ObjFPC as there you can use "Result" as usual (just write ": Variant" as for normal functions).

Regards,
Sven

Reply by email

Mark Morgan Lloyd
to Sven Barth
Sven Barth wrote 12 lines
On 10.08.2013 12:04, Mark Morgan Lloyd wrote: > 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. It seems that the compiler does not detect here that the variants unit is needed and thus does not impolitely add it. Please open a bug report with your code as an example. As a workaround simply add the unit "variants" to the unit where your operator is defined.

http://bugs.freepascal.org/view.php?id=24863

The reason I revisited this was after we were looking at apparent zero-size record problems elsewhere.

Btw: You don't need the "s: Variant" for operators in mode Delphi or mode ObjFPC as there you can use "Result" as usual (just write ": Variant" as for normal functions).

Noted, my "eye was off the ball" while I was struggling with various errors in a test program somewhat larger than I've submitted (working up to 3-D arrays etc.).

Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]

Reply by email