[fpc-pascal] pointer to anything

Frank Peelo f26p at eircom.net
Fri Apr 23 19:16:19 CEST 2010


On 23/04/2010 14:08, spir ☣ wrote:
> On Fri, 23 Apr 2010 08:17:21 -0400
> Doug Chamberlin <dougchamberlin at earthlink.net> wrote:
> 
> 
>>On 4/23/2010 3:33 AM, spir ☣ wrote:
>>
>>>Say I want to implement a kind of linked list which node data may be anything. Thus I cannot store data on place (in nodes), indeed; so it should be referenced. But pointers themselves are supposed to be typed. So, how can I do that?
>>>   
>>
>>The key to solving this problem is to answer the question "How will I 
>>know what type of data that pointer points to?" Once you determine what 
>>is being pointed to you can cast Pointer^ to that type and all should work.
>>
>>I would use Pointer as an untyped pointer to anything and ignore the 
>>warning. Perhaps you can turn off that warning for this code segment 
>>since you are doing this intentionally.

using a cast on a pointer^ means using the pointer^ as an untyped 
pointer to anything but turning off the warning, with the laser-like 
precision of just turning it off for that assignment (or comparison or 
whatever it is you're doing).

But this does depend on the user of the linked list knowing what the 
data type is.

Denis - Would it be at all possible for the objects that you are storing 
all to be derived from a single object type, such as tObject? That would 
let you use the "is" operator, and also to use collections
http://freepascal.org/docs-html/rtl/objects/tcollection.html


> Thank you for both answers. 
> Are there kinds of untyped data structures in the library (such as
> sequence/list, table/mapping/hash/dictionary). I'd like to see how
> they're implemented.
> As a detail, my need is not necessarily the collection to be untyped,
> but the type to be known and/or specified at runtime. So, it is closer
> to what is sometimes called "generics". I want to implement the
> container algorithms once only, since they don't depend on the type
> of the content. Then I may specialise eg a Sequence type into one for
> strings to add a join method.

I think what you are looking for may be possible only with classes, 
unless you roll your own by storing the type of what the pointer points 
to, as an enumerated type value in the record beside the pointer.

If you use classes, maybe something like the class reference type on p67 of
ftp://ftp.freepascal.org/pub/fpc/docs-pdf/ref.pdf
might be useful.

FP




More information about the fpc-pascal mailing list