Sign in Join

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

Mailing lists / fpc-pascal / 2018-11

Dynamic array bug

10 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

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

Ryan Joseph

I finally built the trunk today (version 3.3.1) to try new dynamic array features and a bug fix Sven did a while ago but I’m still getting errors.

Did I get the wrong version or something? I thought these things were working now.

{$mode objfpc}
{$modeswitch advancedrecords}
program general;
type
	TIntArray = array of integer;
	TMyRec = record
		a: TIntArray;
		class operator := (right:TIntArray):TMyRec;
	end;
class operator TMyRec.:= (right:TIntArray):TMyRec;
begin
	result.a := right;
end;
var
	r: TMyRec;
	a: TIntArray;
begin
	r := [1, 2, 3]; // Incompatible types: got "Set Of Byte" expected "TMyRec"
	a := [1, 2, 3];
	a := a + [4]; 	// Operator is not overloaded: "{Dynamic} Array Of LongInt" + "Set Of Byte"
end.

Regards,
Ryan Joseph

silvioprog
to Ryan Joseph

Hi Ryan.

AM Ryan Joseph wrote 5 lines
I finally built the trunk today (version 3.3.1) to try new dynamic array features and a bug fix Sven did a while ago but I’m still getting errors. Did I get the wrong version or something? I thought these things were working now.

Same problem here:

Error: Incompatible types: got "Set Of Byte" expected "TMyRec" Error: Operator is not overloaded: "TIntArray" + "Set Of Byte"

It seems a bug, because the same code works fine on Delphi by changing
operator from ":=" to its respective name "Implicit".

My compiler ver: FPC 3.3.1 [2018/11/07] for x86_64 (Linux)

Silvio Clécio
Ryan Joseph
to silvioprog
On Nov 7, 2018, at 12:57 PM, silvioprog wrote: It seems a bug, because the same code works fine on Delphi by changing operator from ":=" to its respective name "Implicit”.

Good to know. I reported this before and Sven said it was fixed in an update (after another user had submitted the original patch).

Are you able to get a := a + [4]; to work? I’m looking Sven’s old message titled "Feature announcement: Dynamic array extensions” and he says + operator now works.

Regards,
Ryan Joseph

silvioprog
to Ryan Joseph
AM Ryan Joseph wrote 6 lines
Good to know. I reported this before and Sven said it was fixed in an update (after another user had submitted the original patch). Are you able to get a := a + [4]; to work? I’m looking Sven’s old message titled "Feature announcement: Dynamic array extensions” and he says + operator now works.

Yes, it works. But only on Delphi:

a := a + [4];
writeln(a[3]); // prints 4
Silvio Clécio
Ryan Joseph
to silvioprog
On Nov 7, 2018, at 9:31 PM, silvioprog wrote: Yes, it works. But only on Delphi:

That’s too bad, I don’t use Delphi mode. Should be in Objfpc mode also right?

Regards,
Ryan Joseph

silvioprog
to Ryan Joseph
PM Ryan Joseph wrote 8 lines
> On Nov 7, 2018, at 9:31 PM, silvioprog wrote: > > Yes, it works. But only on Delphi: > That’s too bad, I don’t use Delphi mode. Should be in Objfpc mode also right?

Oops... I meant "in Delphi compiler". ^^'

Anyway, it doesn't compile in FPC (tested in both delphi and objfpc modes).

Silvio Clécio
Ryan Joseph
to silvioprog
On Nov 7, 2018, at 10:12 PM, silvioprog wrote: Anyway, it doesn't compile in FPC (tested in both delphi and objfpc modes).

I read the old thread and we need to add {$modeswitch arrayoperators} to make it work. a += [4] does work now.

Also I found the thread where Sven said he fixed the "Incompatible types: got "Set Of Byte” bug in r39554 (https://bugs.freepascal.org/view.php?id=34021). It is indeed fixed but only for + operators.

Do you want me to file a new bug report for := operators?

Regards,
Ryan Joseph

silvioprog
to Ryan Joseph: I read the old thread and we need to add {$modeswitch arrayoperators} to make it work. a…
PM Ryan Joseph wrote 9 lines
I read the old thread and we need to add {$modeswitch arrayoperators} to make it work. a += [4] does work now. Also I found the thread where Sven said he fixed the "Incompatible types: got "Set Of Byte” bug in r39554 ( https://bugs.freepascal.org/view.php?id=34021). It is indeed fixed but only for + operators. Do you want me to file a new bug report for := operators?

You can temporary solve it by specializing a generic array:

program project1;
{$mode objfpc}{$H+}
{$modeswitch advancedrecords}
{$modeswitch arrayoperators}
uses sysutils;
type
  TIntArray = specialize TArray<integer>;
  TMyRec = record
    a: TIntArray;
    class operator := (right: TIntArray): TMyRec;
  end;
class operator TMyRec.:= (right: TIntArray): TMyRec;
begin
  result.a := right;
end;
var
  r: TMyRec;
  a: TIntArray;
begin
  r := specialize TArray<integer>.Create(1, 2, 3);
  a := [1, 2, 3];
  a += [4];
end.
Silvio Clécio
silvioprog
to silvioprog
AM silvioprog wrote 16 lines
On Wed, Nov 7, 2018 at 11:06 PM Ryan Joseph wrote: > I read the old thread and we need to add {$modeswitch arrayoperators} to > make it work. a += [4] does work now. > > Also I found the thread where Sven said he fixed the "Incompatible types: > got "Set Of Byte” bug in r39554 ( > https://bugs.freepascal.org/view.php?id=34021). It is indeed fixed but > only for + operators. > > Do you want me to file a new bug report for := operators? You can temporary solve it by specializing a generic array:

... or forcing the compiler to get the correct type by cast:

type
  TIntArray = array of integer;

...

var
  r: TMyRec;
begin
  r := TIntArray([1, 2, 3]);
...
Silvio Clécio