Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-other / 2012-02

Get/Detect Variable Type

6 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Unknown author

Hi all,
how can i simple get type of variable in freepascal?

For example:

procedure RawWrite(v:anyvariable)
begin
if GETTYPE(v)=string_type then begin;stringwork(v);end;
if GETTYPE(v)=byte_type then begin;dobyte(v);end;
if GETTYPE(v)=pchar_type then...
if GETTYPE(v)=pointer_type then...
etc...
etc...
end;

or use a CASE statement

Is there any way to detect variable type?

Thank you.

Reply by email

Mark Morgan Lloyd
to Unknown author
Show quoted text 19 lines
Hi all, how can i simple get type of variable in freepascal? For example: procedure RawWrite(v:anyvariable) begin if GETTYPE(v)=string_type then begin;stringwork(v);end; if GETTYPE(v)=byte_type then begin;dobyte(v);end; if GETTYPE(v)=pchar_type then... if GETTYPE(v)=pointer_type then... etc... etc... end; or use a CASE statement Is there any way to detect variable type?

How is anyvariable defined? If it's a simple array of bytes or words then there is not, since you've lost the type when assigning to it. If it's a variant record with a tag then you've got the tag. Otherwise consider using variants as supported by the language and RTL.

Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]

Reply by email

Unknown author

Mark Morgan Lloyd wrote the following on 09/02/12 14:10:56:

Show quoted text 25 lines
wrote: > Hi all, > how can i simple get type of variable in freepascal? > > For example: > > procedure RawWrite(v:anyvariable) > begin > if GETTYPE(v)=string_type then begin;stringwork(v);end; > if GETTYPE(v)=byte_type then begin;dobyte(v);end; > if GETTYPE(v)=pchar_type then... > if GETTYPE(v)=pointer_type then... > etc... > etc... > end; > > or use a CASE statement > > > Is there any way to detect variable type? How is anyvariable defined? If it's a simple array of bytes or words then there is not, since you've lost the type when assigning to it. If it's a variant record with a tag then you've got the tag. Otherwise consider using variants as supported by the language and RTL.

I've seen several languages which permitted what the OP asked, but they were all interpreted.

Reply by email

Sven Barth
to Unknown author: how can i simple get type of variable in freepascal? For example: procedure…

Am 09.02.2012 14:49, schrieb

Show quoted text 20 lines
Hi all, how can i simple get type of variable in freepascal? For example: procedure RawWrite(v:anyvariable) begin if GETTYPE(v)=string_type then begin;stringwork(v);end; if GETTYPE(v)=byte_type then begin;dobyte(v);end; if GETTYPE(v)=pchar_type then... if GETTYPE(v)=pointer_type then... etc... etc... end; or use a CASE statement Is there any way to detect variable type?

The best you can do is using Variant as variable type. See here: http://www.freepascal.org/docs-html/ref/refse18.html#refsu20.html Also take a look at unit Variants (for which no documentation exists yet though).

Regards,
Sven

Reply by email

Mario Ray Mahardhika
to Sven Barth

Suggestion:
Overload the procedure for each type you'd like to support instead.

________________________________

Earlier message 35 lines
From: Sven Barth To: Sent: Thursday, February 9, 2012 10:57 PM Subject: Re: [fpc-other] Get/Detect Variable Type Am 09.02.2012 14:49, schrieb > > Hi all, >    how can i simple get type of variable in freepascal? > > For example: > > procedure RawWrite(v:anyvariable) > begin > if GETTYPE(v)=string_type then begin;stringwork(v);end; > if GETTYPE(v)=byte_type then begin;dobyte(v);end; > if GETTYPE(v)=pchar_type then... > if GETTYPE(v)=pointer_type then... > etc... > etc... > end; > > or use a CASE statement > > > Is there any way to detect variable type? The best you can do is using Variant as variable type. See here: http://www.freepascal.org/docs-html/ref/refse18.html#refsu20.html Also take a look at unit Variants (for which no documentation exists yet though). Regards, Sven

Reply by email

Unknown author
to Mario Ray Mahardhika

Thank you all pointing me out to Variant.

Thank you and have a nice day.

______________________________________________________________

Show quoted text 55 lines
Od: "Mario Ray Mahardhika" Komu: "Other FPC related discussions." Datum: 10.02.2012 03:20 Předmět: Re: [fpc-other] Get/Detect Variable Type Suggestion: Overload the procedure for each type you'd like to support instead. ________________________________ From: Sven Barth To: Sent: Thursday, February 9, 2012 10:57 PM Subject: Re: [fpc-other] Get/Detect Variable Type Am 09.02.2012 14:49, schrieb > > Hi all, >    how can i simple get type of variable in freepascal? > > For example: > > procedure RawWrite(v:anyvariable) > begin > if GETTYPE(v)=string_type then begin;stringwork(v);end; > if GETTYPE(v)=byte_type then begin;dobyte(v);end; > if GETTYPE(v)=pchar_type then... > if GETTYPE(v)=pointer_type then... > etc... > etc... > end; > > or use a CASE statement > > > Is there any way to detect variable type? The best you can do is using Variant as variable type. See here: http://www.freepascal.org/docs-html/ref/refse18.html#refsu20.html Also take a look at unit Variants (for which no documentation exists yet though). Regards, Sven _______________________________________________ fpc-other maillist  -  http://lists.freepascal.org/mailman/listinfo/fpc-other ---------- _______________________________________________ fpc-other maillist - http://lists.freepascal.org/mailman/listinfo/fpc-other

Reply by email