[fpc-pascal] How to know the string type of a variable?

Marcos Douglas md at delfire.net
Mon Dec 30 15:32:11 CET 2013


On Mon, Dec 30, 2013 at 11:08 AM, Michael Van Canneyt
<michael at freepascal.org> wrote:
>
>
> On Mon, 30 Dec 2013, Marcos Douglas wrote:
>
>> On Mon, Dec 30, 2013 at 9:48 AM, Marcos Douglas <md at delfire.net> wrote:
>>>
>>> On Mon, Dec 30, 2013 at 9:41 AM, Michael Van Canneyt
>>> <michael at freepascal.org> wrote:
>>>>
>>>>
>>>> On Mon, 30 Dec 2013, Marcos Douglas wrote:
>>>>
>>>>> [...]
>>
>> Well, didn't worked as I wanted.
>> For example, I know UTF8String is an AnsiString but the TypeInfo
>> should catch UTF8String not AnsiString, doesn't?
>
>
> No. You make a wrong assumption.
>
> TypeInfo will return the DECLARED type of S.
>
> Not the type that was actually passed when calling the routine:
> That has been converted to the declared type of S by the compiler even
> before the routine ShowType is called.

You're right.
Well is there another way to do I showed before?
I want to create a record/object type to receive a "string" but I need
to know which string type was before.

If I change the code (see below) to use Pointer type it works but I
think this is won't help me.

=== BEGIN ===
procedure TForm1.Button1Click(Sender: TObject);
var
  AStr: AnsiString;
  U8Str: UTF8String;
  UniStr: UnicodeString;

  function ShowType(P: Pointer): string;
  begin
    if P = TypeInfo(AnsiString) then
      ShowMessage('AnsiString')
    else if P = TypeInfo(UTF8String) then
      ShowMessage('UTF8String')
    else if P = TypeInfo(UnicodeString) then
      ShowMessage('UnicodeString')
  end;

begin
  ShowType(TypeInfo(AStr));
  ShowType(TypeInfo(U8Str));
  ShowType(TypeInfo(UniStr));
end;
=== END ===

Regards,
Marcos Douglas



More information about the fpc-pascal mailing list