Recent

Author Topic: Incompatible types: got "Set Of Byte" expected "{Dynamic} Array Of LongInt"  (Read 2399 times)

quake004

  • New Member
  • *
  • Posts: 33
I'm just trying to pass an array to a function through another. I don't know if I've declared or passed the array in the wrong way. How would I do this?

Code: Pascal  [Select][+][-]
  1. program Test;
  2.  
  3. {$MODE OBJFPC}
  4.  
  5. function DoSomething(arr: Array of Integer): Boolean;
  6. var
  7.   i: Integer;
  8. begin
  9.   for  i in arr  do
  10.     WriteLn(i);
  11.     ReadLn;
  12. end;
  13.  
  14. procedure pass(arr: Array of Integer);
  15. begin
  16.   DoSomething(arr);
  17. end;
  18.  
  19. procedure Main;
  20. var
  21.   arr: Array of Integer;
  22. begin
  23.   arr := [1, 2, 3];
  24.   pass(arr);
  25. end;
  26.  
  27. begin
  28.   Main();
  29. end.
  30.  

Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
Code: Pascal  [Select][+][-]
  1. program Test;
  2.  
  3. {$MODE OBJFPC}
  4.  
  5. type
  6.   TMyArray = array of Integer;
  7.  
  8. function DoSomething(arr: TMyArray): Boolean;
  9. var
  10.   i: Integer;
  11. begin
  12.   for  i in arr  do
  13.     WriteLn(i);
  14.     ReadLn;
  15. end;
  16.  
  17. procedure pass(arr: TMyArray);
  18. begin
  19.   DoSomething(arr);
  20. end;
  21.  
  22. procedure Main;
  23. var
  24.   arr: TMyArray;
  25. begin
  26.   SetLength(arr, 3);
  27.   arr[0] := 1;
  28.   arr[1] := 2;
  29.   arr[2] := 3;
  30.   pass(arr);
  31. end;
  32.  
  33. begin
  34.   Main();
  35. end.

I already told you some days ago, it seems you didn't pay attention.

Note:
Your code still has a minor issue.
« Last Edit: April 19, 2020, 04:57:33 pm by Handoko »

Thaddy

  • Hero Member
  • *****
  • Posts: 14210
  • Probably until I exterminate Putin.
days ago, it seems you didn't pay attention.
Well. his code compiles in trunk and 3.2.0. (With a warning: the function has no result!!)
There are some user changes in the mean time (after 3.0.4).
Your code is for 3.0.4. and also works, that is true. I was writing a similar answer, but posts crossed and I did test it for trunk, hence I know his code will work in recent - not officially released yet -compilers.
« Last Edit: April 19, 2020, 04:50:53 pm by Thaddy »
Specialize a type, not a var.

howardpc

  • Hero Member
  • *****
  • Posts: 4144
The compiler considers each parameter type "array of Integer" as a different type, and so incompatible, although that seems unintuitive to newcomers.
Better to consistently use a type pre-declared in Types:
Code: Pascal  [Select][+][-]
  1. program project1;
  2.  
  3. {$MODE OBJFPC}
  4.  
  5. uses
  6.   Types;
  7.  
  8. procedure DoSomething(arr: TIntegerDynArray);
  9. var
  10.   i: Integer;
  11. begin
  12.   for i in arr do
  13.     WriteLn(i);
  14.   ReadLn;
  15. end;
  16.  
  17. procedure pass(arr: TIntegerDynArray);
  18. begin
  19.   DoSomething(arr);
  20. end;
  21.  
  22. procedure Main;
  23. var
  24.   arr: TIntegerDynArray;
  25. begin
  26.   arr := [1, 2, 3];
  27.   pass(arr);
  28. end;
  29.  
  30. begin
  31.   Main();
  32. end.
« Last Edit: April 19, 2020, 05:49:40 pm by howardpc »

Thaddy

  • Hero Member
  • *****
  • Posts: 14210
  • Probably until I exterminate Putin.
The compiler considers each parameter type "array of Integer" as a different type, and so incompatible, although that seems unintuitive to newcomers.
As I explained: this has been corrected in newer versions. The clash between open array and dynamic array has been resolved
.
Specialize a type, not a var.

Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
@Thaddy

Did you mean the newer version(s) support array of something for the parameter directly? Well, interesting. Thank you for the information.

Thaddy

  • Hero Member
  • *****
  • Posts: 14210
  • Probably until I exterminate Putin.
@Thaddy

Did you mean the newer version(s) support array of something for the parameter directly? Well, interesting. Thank you for the information.
See above answer: you can both pass as open array and dynamic array.
Although I agree with you and Howard that it is better to distinguish between the two and declare a separate type ( as we used to do)
« Last Edit: April 19, 2020, 05:01:59 pm by Thaddy »
Specialize a type, not a var.

Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
I love this forum, there always something new to learn on every visit.

quake004

  • New Member
  • *
  • Posts: 33

I already told you some days ago, it seems you didn't pay attention.

Note:
Your code still has a minor issue.

I thought it was only for records or functions. Thanks all.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
The compiler considers each parameter type "array of Integer" as a different type, and so incompatible, although that seems unintuitive to newcomers.

No. If you have a literal array of Integer parameter then it will be assignment compatible to other array of Integer parameters (these are open array parameters). However if you have a dynamic array in the mix (this can only be the case if you have a named type aka TIntegerArray = array of Integer as array parameter or you have a dynamic array variable - like the arr in quake004's Main procedure), then you can pass such a dynamic array to an open array parameter, but not an open array to a dynamic array parameter. (Also if you have multiple different dynamic array types of the same element type, they'll be assignment compatible as well)

What is the problem in quake004's code is the initialization of the dynamic array variable in Main:

Code: Pascal  [Select][+][-]
  1. procedure Main;
  2. var
  3.   arr: Array of Integer;
  4. begin
  5.   arr := [1, 2, 3];
  6.   pass(arr);
  7. end;

FPC 3.0.4 does not yet support dynamic array constructors (or more precisely: it only supports the Create one for named dynamic arrays), thus the compiler parses the right side of the assignment as a set, resulting in the compiler trying to assign a set to a dynamic array. Thus the error mentioned in the topic: Incompatible types: got "Set Of Byte" expected "{Dynamic} Array Of LongInt".

The only change necessary is how Handoko changed the initialization of arr in Main:

Code: Pascal  [Select][+][-]
  1. procedure Main;
  2. var
  3.   arr: array of Integer;
  4. begin
  5.   SetLength(arr, 3);
  6.   arr[0] := 1;
  7.   arr[1] := 2;
  8.   arr[2] := 3;
  9.   pass(arr);
  10. end;

 

TinyPortal © 2005-2018