Sign in Join

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

Mailing lists / fpc-pascal / 2010-01

Delphi mode syntax error

10 messages · 4 people ·

Reply by email Subscribe to this list Your subscription

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

Roland Turcan

Hello all,

I use Delphi style of programing in Lazarus to keep a compatibility with Kylix and therefore I want to get this code which is valid of Freepascal's ObjectPascal, but in {$MODE Delphi} it doesn't accept calling of "MyItems[index]" and writes "Array type required".

How to adapt this code to be valid for Delphi mode.

TYPE
  PtrUInt = DWord;
  TMyObj  = PtrUInt;
  PMyObj  = ^TMyObj;
  TArr    = ARRAY OF TMyObj;
procedure TForm1.FormCreate(Sender: TObject);
VAR MyItems         :PMyObj;
    Arr             :TArr;
begin
  SetLength (Arr, 5);
  Arr[0] := 1;
  Arr[1] := 2;
  Arr[2] := 3;
  Arr[3] := 4;
  Arr[4] := 5;
  MyItems := Pointer (Arr);
  ShowMessage (IntToStr (MyItems[0]));
end;

Thanks.

Reply by email

Jonas Maebe
to Roland Turcan
Roland Turcan wrote 6 lines
I use Delphi style of programing in Lazarus to keep a compatibility with Kylix and therefore I want to get this code which is valid of Freepascal's ObjectPascal, but in {$MODE Delphi} it doesn't accept calling of "MyItems[index]" and writes "Array type required". How to adapt this code to be valid for Delphi mode.

Since you posted an incomplete sample, I can only guess, but...

ShowMessage (IntToStr (MyItems[0]));
I think this should work:
   ShowMessage (IntToStr (MyItems[0]()));

Please also file a bug report with a compilable example (which does not depend on Lazarus) that demonstrates the problem, although some things have been fixed recently regarding procvars in Delphi mode and it may already work with svn trunk.

Jonas

Reply by email

Jonas Maebe
to Jonas Maebe
Jonas Maebe wrote 9 lines
On 19 Jan 2010, at 11:14, Roland Turcan wrote: > ShowMessage (IntToStr (MyItems[0])); I think this should work: ShowMessage (IntToStr (MyItems[0]())); Please also file a bug report with a compilable example (which does not depend on Lazarus) that demonstrates the problem

Well, at least if the original code compiles with Delphi/Kylix.

Jonas

Reply by email

Roland Turcan
to Jonas Maebe

<<< 19.01.2010 11:39 - Jonas Maebe "" >>>

Jonas Maebe wrote 9 lines
> On 19 Jan 2010, at 11:14, Roland Turcan wrote: > >> ShowMessage (IntToStr (MyItems[0])); > > I think this should work: > ShowMessage (IntToStr (MyItems[0]())); > > Please also file a bug report with a compilable example (which does > not depend on Lazarus) that demonstrates the problem

JM> Well, at least if the original code compiles with Delphi/Kylix.

Thank Jonas for you reply, but FreePascal and Kylix as well reports the same error.

Reply by email

Michael Van Canneyt
to Roland Turcan
Roland Turcan wrote 18 lines
<<< 19.01.2010 11:39 - Jonas Maebe "" >>> JM> On 19 Jan 2010, at 11:27, Jonas Maebe wrote: >> On 19 Jan 2010, at 11:14, Roland Turcan wrote: >> >>> ShowMessage (IntToStr (MyItems[0])); >> >> I think this should work: >> ShowMessage (IntToStr (MyItems[0]())); >> >> Please also file a bug report with a compilable example (which does >> not depend on Lazarus) that demonstrates the problem JM> Well, at least if the original code compiles with Delphi/Kylix. Thank Jonas for you reply, but FreePascal and Kylix as well reports the same error.

It should not compile in Delphi mode.
Array syntax is not supported by Delphi.

So there is no bug.

Michael.

Reply by email

Jonas Maebe
to Michael Van Canneyt
Michael Van Canneyt wrote
It should not compile in Delphi mode. Array syntax is not supported by Delphi.

Oops, only now I see he was trying to index a pointer like an array. And even in FPC I would very strongly recommend against typecasting a dynamic array to a pointer and then performing operations using this pointer. Dynamic arrays are opaque structures and trying to directly access the underlying data should never be done.

Jonas

Reply by email

Roland Turcan
to Jonas Maebe

<<< 19.01.2010 13:10 - Jonas Maebe "" >>>

Michael Van Canneyt wrote
> It should not compile in Delphi mode. > Array syntax is not supported by Delphi.

JM> Oops, only now I see he was trying to index a pointer like an array. JM> And even in FPC I would very strongly recommend against typecasting a JM> dynamic array to a pointer and then performing operations using this JM> pointer. Dynamic arrays are opaque structures and trying to directly JM> access the underlying data should never be done.

Hello Jonas,

But this ways is already used in LCL/FPC source as well.

Reply by email

Jonas Maebe
to Roland Turcan
Roland Turcan wrote 13 lines
<<< 19.01.2010 13:10 - Jonas Maebe "" >>> JM> Oops, only now I see he was trying to index a pointer like an array. JM> And even in FPC I would very strongly recommend against typecasting a JM> dynamic array to a pointer and then performing operations using this JM> pointer. Dynamic arrays are opaque structures and trying to directly JM> access the underlying data should never be done. But this ways is already used in LCL/FPC source as well.

I don't know about the LCL, but I guess in the FPC sources the only place where this is done is inside the RTL helpers that are used to manage the dynamic arrays. These by definition depend on the internal layout of the dynamic array and will be updated if something would change about dynamic arrays.

I also don't see any advantage to using a pointer instead of a dynamic array directly. It will not be faster in any way, since there are no uniqueness checks for dynamic arrays. In fact, it will probably be slower, since you now have two variables in use: one to hold a reference to the dynamic array, and one for the extra pointer.

Jonas

Reply by email

Marco van de Voort
to Jonas Maebe

In our previous episode, Jonas Maebe said:

Show quoted text 5 lines
I also don't see any advantage to using a pointer instead of a dynamic array directly. It will not be faster in any way, since there are no uniqueness checks for dynamic arrays. In fact, it will probably be slower, since you now have two variables in use: one to hold a reference to the dynamic array, and one for the extra pointer.

Hmm, under delphi at least, it is cheaper to do

var pstart,pend : PSomeType;
if length(sometypedynarray)>0 then
  begin
    ;
    peind (sometypedynarray)-1];
    while (pstart<=peind) do
      begin
        operate_on(pstart);
        inc(pstart);
      end;
  end;

then for i over a dynarray, even if sometype is a simple type.

Reply by email

Jonas Maebe
to Marco van de Voort
Marco van de Voort wrote 16 lines
Hmm, under delphi at least, it is cheaper to do var pstart,pend : PSomeType; if length(sometypedynarray)>0 then begin ; peind (sometypedynarray)-1]; while (pstart<=peind) do begin operate_on(pstart); inc(pstart); end; end; then for i over a dynarray, even if sometype is a simple type.

I can't imagine why that would be any faster than a simple for-loop, except for an implementation detail in the code generator or optimiser of the tested Delphi version(s) (or in case range checking is enabled -- but in that case using pointers to work around range checking would be strange). And it most certainly looks a lot worse than

for i:=0 to high(sometypedynarray) do
   operate_on(sometypedynarray[i]);

It would also surprise me if any speed difference that may exist were significant.

Jonas

Reply by email