for-in loop cast

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

for-in loop cast

Ryan Joseph-2
Question I’ve always had. Why do I need to cast “value” to “pointer" otherwise I get: Incompatible types: got "Pointer" expected “TObject” error?. I don’t find this very helpful and it doesn’t really make sense even.

var
  list: TObjectList;
  value: TObject;
begin
  for pointer(value) in list do
    ;


Regards,
        Ryan Joseph

_______________________________________________
fpc-pascal maillist  -  [hidden email]
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: for-in loop cast

Ben Grasset
 On Thu, Sep 26, 2019 at 11:37 AM Ryan Joseph <[hidden email]> wrote:
Question I’ve always had. Why do I need to cast “value” to “pointer" otherwise I get: Incompatible types: got "Pointer" expected “TObject” error?. I don’t find this very helpful and it doesn’t really make sense even.

Well, TObjectList is a descendant of TList, which is of course a non-generic list of void pointers. The enumerator for TList that allows the for-in loop to work, called TListEnumerator, as such obviously implements GetCurrent as returning "pointer".

TObjectList doesn't have its own enumerator implementation on top of the TList one or anything like that, so when you use for-in on TObjectList, you're using the pointer-based TList one.

The fix for this is simple IMO: use a generic list class instead. For example, if you want something that works exactly like your code example, you can specialize TObjectList from Generics.Collections literally *with* TObject:

type
  TBaseObjectList = TObjectList<TObject>;
var
  list: TBaseObjectList;
  value: TObject;
begin
  for pointer(value) in list do
    ;  

_______________________________________________
fpc-pascal maillist  -  [hidden email]
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: for-in loop cast

Free Pascal - General mailing list
In reply to this post by Ryan Joseph-2
Ryan Joseph <[hidden email]> schrieb am Do., 26. Sep. 2019, 17:37:
Question I’ve always had. Why do I need to cast “value” to “pointer" otherwise I get: Incompatible types: got "Pointer" expected “TObject” error?. I don’t find this very helpful and it doesn’t really make sense even.

var
  list: TObjectList;
  value: TObject;
begin
  for pointer(value) in list do
    ;

Aside from what Ben wrote I wonder whether such a cast should be able to compile at all... 🤔 

And we should probably add a proper TObject enumerator to TObjectList. 

Regards, 
Sven 

_______________________________________________
fpc-pascal maillist  -  [hidden email]
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: for-in loop cast

Ben Grasset
In reply to this post by Ben Grasset
On Thu, Sep 26, 2019 at 11:59 AM Ben Grasset <[hidden email]> wrote:
-snip-

I meant to change the last part of the revised code example to:

  for value in list do

because obviously the pointer cast is no longer necessary in that case.

_______________________________________________
fpc-pascal maillist  -  [hidden email]
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: for-in loop cast

Ryan Joseph-2
In reply to this post by Free Pascal - General mailing list


> On Sep 26, 2019, at 1:34 PM, Sven Barth via fpc-pascal <[hidden email]> wrote:
>
> And we should probably add a proper TObject enumerator to TObjectList.
>

I just assumed that had to be there! I’ll make a generic replacement like Ben suggests.

Regards,
        Ryan Joseph

_______________________________________________
fpc-pascal maillist  -  [hidden email]
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal