How to get option from pointer in Delphi?

I need to convert a bare pointer to an option. I know that the pointer points to an option, but I cannot return it. A direct cast (as I thought quite a lot) fails:

Result := Variant(FAddress)^ 

returns compiler error: [DCC error] E2089 Invalid type

I also looked at the options.pas block, but nothing jumped at me.

Obviously I'm missing something. What is the way to do this?

+6
source share
1 answer

If the pointer points to a variant, then its type is PVariant. Add it to this, and then look for:

 Result := PVariant(FAddress)^; 

Better yet, declare FAddress with the correct type to start, and then you don't need to infer the type:

 var FAddress: PVariant; Result := FAddress^; 

The compiler considers your type attempts to be invalid because Variant is a larger type than a pointer. The compiler does not know where to get additional data to create the full Variant value. And if the cast type was valid, the use of the ^ operator is in any case not allowed on Variant. You may have dealt with this:

 Result := Variant(FAddress^); 

I never liked it; if FAddress is an untyped pointer, then dereferencing it gives a value without any size or type at all, and it is just weird to print such things.

+16
source

Source: https://habr.com/ru/post/885852/


All Articles