Recent

Author Topic: [SOLVED]FreePascal did not support type-hard-translation?  (Read 6190 times)

lukenothing

  • New Member
  • *
  • Posts: 12
[SOLVED]FreePascal did not support type-hard-translation?
« on: October 30, 2012, 10:37:43 am »
In delphi 7, it will be passed by compiler.
Code: [Select]
var
    a: SELF_CLASS;
    i: Integer;
begin

    for i := 0 to AList.Count-1 do begin
       a := AList.Items[i];  //  <========In delphi, It's OK!!!
    end;
end;

But in FreePascal, it gives me a error as below message:
Code: [Select]
abctest.pas(136,23) Error: Incompatible types: got "Pointer" expected "SELF_CLASS"

What should I do?
« Last Edit: November 01, 2012, 01:00:15 am by lukenothing »

Zoran

  • Hero Member
  • *****
  • Posts: 1940
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: FreePascal did not support type-hard-translation?
« Reply #1 on: October 30, 2012, 10:46:42 am »
Try (untested):
Code: [Select]
       a := SELF_CLASS(AList.Items[i]);
Swan, ZX Spectrum emulator https://github.com/zoran-vucenovic/swan

lukenothing

  • New Member
  • *
  • Posts: 12
Re: FreePascal did not support type-hard-translation?
« Reply #2 on: October 30, 2012, 11:04:35 am »
Try (untested):
Code: [Select]
       a := SELF_CLASS(AList.Items[i]);
Thank you !

It's Ok. But I doubt that it created a new object, actually, I want an original object.

Is it quite difference between delphi and freePascal? In delphi, I guest a class is just a Pointer to a object, so that it supports set the value directly.

I just read a little online help of FreePascal. It seems that it fufilled it as C language.

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: FreePascal did not support type-hard-translation?
« Reply #3 on: October 30, 2012, 11:54:07 am »
Code: [Select]
var
    a: SELF_CLASS;
    i: Integer;
begin
    for i := 0 to AList.Count-1 do begin
       a := AList.Items[i];
    end;
end;
Added code-tags for readability. (Aren't there forum moderators that would do it?)

What are you trying to do? The code is overwriting variable a for no seeming purpose. AList.Items{i} returns a string, if AList.Items is based on TStrings. How did you define SELF_CLASS type?
« Last Edit: October 30, 2012, 11:58:42 am by User137 »

Zoran

  • Hero Member
  • *****
  • Posts: 1940
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: FreePascal did not support type-hard-translation?
« Reply #4 on: October 30, 2012, 04:45:00 pm »
What are you trying to do? The code is overwriting variable a for no seeming purpose. AList.Items{i} returns a string, if AList.Items is based on TStrings. How did you define SELF_CLASS type?

I guess AList is of TList type. So items are pointers - http://www.freepascal.org/docs-html/rtl/classes/tlist.items.html.

It's Ok. But I doubt that it created a new object, actually, I want an original object.

No, it does not create a new object. When created, objects are allocated on the heap, so they are always accessed through pointers (implicitly).
What happens here is - you get a pointer and then do a typecast, so that it can be assigned to your variable of SELF_CLASS type (which is implicitly a pointer type!), that is all, no new object is created.
Swan, ZX Spectrum emulator https://github.com/zoran-vucenovic/swan

lukenothing

  • New Member
  • *
  • Posts: 12
Re: FreePascal did not support type-hard-translation?
« Reply #5 on: October 31, 2012, 03:43:36 am »
Many thanks to all !!!

Yes, as Zoran said, AList is a object of TList class.

Sorry about that I didn't make my code clean and right, Zoran typed is right.
Code: [Select]
a := AList.Items[i];
« Last Edit: November 01, 2012, 01:00:53 am by lukenothing »

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12152
  • FPC developer.
Re: [SOLVED]FreePascal did not support type-hard-translation?
« Reply #6 on: October 31, 2012, 09:04:16 am »
In delphi 7, it will be passed by compiler.

I doubt Delphi 7 compiles incomplete code. I think you are making an error somewhere, and the simplified and incomplete code you post here is not the same as you use in Delphi.
 
If you think there is a real difference, reduce it to a minimal compiling program, test with both D7 and FPC, and then submit it to the bugtracker.

lukenothing

  • New Member
  • *
  • Posts: 12
Re: [SOLVED]FreePascal did not support type-hard-translation?
« Reply #7 on: November 01, 2012, 12:56:58 am »
In delphi 7, it will be passed by compiler.

I doubt Delphi 7 compiles incomplete code. I think you are making an error somewhere, and the simplified and incomplete code you post here is not the same as you use in Delphi.
 
If you think there is a real difference, reduce it to a minimal compiling program, test with both D7 and FPC, and then submit it to the bugtracker.

Sorry about my mistake.
The right code should be this one:
Code: [Select]
a := AList.Items[i];

Not this one:
Code: [Select]
a := AList.Items;

Oh my god, I just found the mistake, I use the 'quote', it will filiter '[' and ']' after the 'AList.Items'...now I replace it as 'code'.
« Last Edit: November 01, 2012, 12:59:26 am by lukenothing »

 

TinyPortal © 2005-2018