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

Michael Van Canneyt michael at freepascal.org
Mon Dec 30 15:47:13 CET 2013



>> 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.

There is no way.

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

No, it will not help you.

One thing you can do is use overloading:

TStringType = (stAnsi,stUnicode,stUTF8);

Procedure DoSomeStringOperation(S : Pointer; AStringType : TStringType);

begin
   // Whatever
end;

Procedure DoSomeStringOperation(S : Ansistring);

begin
   DoSomeStringOperation(Pointer(S),stAnsi);
end;

Procedure DoSomeStringOperation(S : UnicodeString);

begin
   DoSomeStringOperation(Pointer(S),stUnicode);
end;

Procedure DoSomeStringOperation(S : UTF8String);

begin
   DoSomeStringOperation(Pointer(S),stUTF8);
end.

The compiler will choose the correct overloaded version, and you will know which one she picked.

But it would help to know what exactly you want to achieve.

Michael.



More information about the fpc-pascal mailing list