<div dir="ltr"><div dir="ltr"><div> On Thu, Sep 26, 2019 at 11:37 AM Ryan Joseph <<a href="mailto:genericptr@gmail.com">genericptr@gmail.com</a>> wrote:</div></div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">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.<br></blockquote><div><br></div>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".<div><br></div><div>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.</div><div><br></div><div>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:</div><div><br></div><div>type</div><div> TBaseObjectList = TObjectList<TObject>;</div><div>var<br> list:
TBaseObjectList;<br> value: TObject;<br>begin<br> for pointer(value) in list do<br></div><div> ; </div></div></div>