typeof

New to FreeBASIC? Post your questions here.
Juergen Kuehlwein
Posts: 284
Joined: Mar 07, 2018 13:59
Location: Germany

typeof

Post by Juergen Kuehlwein »

I understand that i can make use "typeof" for identifying a variables´s type like this:

Code: Select all

dim s as string = "123"
dim w as wstring * 16 = "456"
dim z as wstring * 16 = "789"
dim r as tagrect


#if typeof(s) = string
  MessageBox 0, "String", "test", MB_OK
#endif   


#if typeof(w) = wstring
  MessageBox 0, "Wstring", "test", MB_OK
#endif   


#if typeof(z) = zstring
  MessageBox 0, "Zstring", "test", MB_OK
#endif


#if typeof(r) = tagrect
  MessageBox 0, "Rect", "test", MB_OK
#endif   

So "STRING" is recognized (like all other numeric and user defined data types btw.), but why do "WSTRING" and "ZSTRING" fail? Is there another method of identifying a variable´s type from it´s name (identifier) at run time or compile time ?


Thanks


JK
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: typeof

Post by fxm »

The right syntax is:
#if typeof(variable_name) = typeof(data_type)

Code: Select all

dim s as string = "123"
dim w as wstring * 16 = "456"
dim z as zstring * 16 = "789"
dim r as tagrect

#if typeof(s) = typeof(string)
  MessageBox 0, "String", "test", MB_OK
#endif   


#if typeof(w) = typeof(wstring * 16)
  MessageBox 0, "Wstring", "test", MB_OK
#endif   


#if typeof(z) = typeof(zstring * 16)
  MessageBox 0, "Zstring", "test", MB_OK
#endif


#if typeof(r) = typeof(tagrect)
  MessageBox 0, "Rect", "test", MB_OK
#endif   
Last edited by fxm on May 01, 2018 16:55, edited 1 time in total.
St_W
Posts: 1626
Joined: Feb 11, 2009 14:24
Location: Austria
Contact:

Re: typeof

Post by St_W »

You can instruct the compiler print the typeof(...) results at compilation time using

Code: Select all

#print typeof(myVariable)
You'll see that the type of e.g. a "zstring*5" is "ZSTRING * 5" (and not "ZSTRING"). You can compare the type by using typeof on both sides, but I can't think of a solution for matching any length of ZStrings.

Code: Select all

dim a as zstring * 5
#if typeof(a) = typeof(zstring * 5)
#print "match"
#endif
Juergen Kuehlwein
Posts: 284
Joined: Mar 07, 2018 13:59
Location: Germany

Re: typeof

Post by Juergen Kuehlwein »

Thanks guys, you got me thinking ....

this works for "WSTRING" (but alas, it still doesn´t for "ZSTRING"):

Code: Select all

dim s as string = "123"
dim w as wstring * 16 = "456"
dim z as wstring * 16 = "789"
dim r as tagrect


#if typeof(s) = string
  MessageBox 0, "String", "test", MB_OK
#endif   


#if typeof(@w) = typeof(wstring ptr)
  MessageBox 0, "Wstring", "test", MB_OK
#endif   


#if typeof(@z) = typeof(zstring ptr)
  MessageBox 0, "Zstring", "test", MB_OK
#endif   


#if typeof(r) = tagrect
  MessageBox 0, "rect", "test", MB_OK
#endif   


more ideas ?


Thanks


JK
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: typeof

Post by fxm »

Correct your typo:
dim z as wstring * 16 = "789"
dim z as zstring * 16 = "789"
St_W
Posts: 1626
Joined: Feb 11, 2009 14:24
Location: Austria
Contact:

Re: typeof

Post by St_W »

nice idea to work around the length issue by using pointer types :-)
Juergen Kuehlwein
Posts: 284
Joined: Mar 07, 2018 13:59
Location: Germany

Re: typeof

Post by Juergen Kuehlwein »

Thanks - problem solved!

I just found out myself, i had a typo, but you were faster :-)


JK
Juergen Kuehlwein
Posts: 284
Joined: Mar 07, 2018 13:59
Location: Germany

Re: typeof

Post by Juergen Kuehlwein »

Next question regarding "TypeOf":


I can use "#print TypeOf(any data type)", but is there a way to get then name of the corresponding data type (e.g "STRING", "LONG" etc.) into a string at runtime? Something like: "mystring = TypeOf(x)" (which doesn´t compile), where "x" is a "LONG" for instance.


Thanks


JK
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: typeof

Post by fxm »

TypeOf() is a compiler intrinsic that can be used only at compilation time (replacing itself with the type of the variable passed to it).
It cannot be used at execution time.
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: typeof

Post by fxm »

The only variables whose dynamic type could be retrieved (but with great difficulty if the list of possible types is unknown at compile time) during execution are only instances of user objects being defined as directly or indirectly derived from the built-in type OBJECT, because only the built-in type OBJECT provides Run-Time Type Information (RTTI).

But the list of possible static types being known at compile time, the problem to retrieve these only static types as strings at run time from the variable names is very easy to code.

See the examples in my following posts.
Last edited by fxm on May 02, 2018 8:14, edited 8 times in total.
Juergen Kuehlwein
Posts: 284
Joined: Mar 07, 2018 13:59
Location: Germany

Re: typeof

Post by Juergen Kuehlwein »

Ok!

Is there another way to get then name of the corresponding data type (not only the standard types, but also user defined types) into a string at run time? Or maybe somehow set the value of a string variable at compile time with the corresponding data type, so it is available at run time.

In other words, how to get the name of an arbitrary variable´s data type into a string variable (any of these will do: STRING, ZSTRING, WSTRING, or any pointer to the string data), which can be accessed at run time. Obviously the variable´s name and data type is known at compile time.


Any ideas ?


JK
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: typeof

Post by fxm »

I think I answered a little in my last message (just above).
Juergen Kuehlwein
Posts: 284
Joined: Mar 07, 2018 13:59
Location: Germany

Re: typeof

Post by Juergen Kuehlwein »

Yes, thanks - we cross posted!

It isn´t a "must have" for me, but it would be a "nice to have". So maybe someone still has yet another idea for achieving, what i want somehow or other...


Thanks


JK
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: typeof

Post by fxm »

From the list of possible static types known at compile time, an example to retrieve these only static types as strings at run time from the variable names:

Code: Select all

Function staticTypeName overload (Byref i As Integer) As String
  Return "Integer"
End Function

Function staticTypeName overload (Byref d As Double) As String
  Return "Double"
End Function

Function staticTypeName overload (Byref s As String) As String
  Return "String"
End Function

'.....
'.....

'--------------------------------------------------------

Dim As Double d0
Print staticTypeName(d0)

Sleep

Code: Select all

Double
Last edited by fxm on May 02, 2018 6:02, edited 1 time in total.
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: typeof

Post by fxm »

Example with static and dynamic types (dynamic types can be tested with the IS keyword):

Code: Select all

Type simpleUDT
  Dim As Longint l
End Type

Type UDT Extends Object
End Type

Type derivedUDT Extends UDT
End Type

Function staticTypeName overload (Byref i As Integer) As String
  Return "Integer"
End Function

Function staticTypeName overload (Byref d As Double) As String
  Return "Double"
End Function

Function staticTypeName overload (Byref s As String) As String
  Return "String"
End Function

Function staticTypeName overload (Byref su0 As simpleUDT) As String
  Return "simpleUDT"
End Function

Function staticTypeName overload (Byref o As Object) As String
  Return "Object"
End Function

Function staticTypeName overload (Byref u As UDT) As String
  Return "UDT"
End Function

Function staticTypeName overload (Byref du As derivedUDT) As String
  Return "derivedUDT"
End Function

Function dynamicTypeName overload (Byref o As Object) As String
  If o Is derivedUDT Then Return "derivedUDT"
  If o Is UDT Then Return "UDT"
  Return "Object"
End Function

'.....
'.....

'--------------------------------------------------------

Dim As Integer i0                '' Integer type variable (only static type)
Dim As Double d0                 '' Double type variable (only static type)
Dim As String s0                 '' String type variable (only static type)
Dim As simpleUDT su0             '' simpleUDT type variable (only static type)
Dim As Object o0                 '' Object type variable (only static type)
Dim As UDT u0                    '' UDT type variable (only static type)
Dim As derivedUDT du0            '' derivedUDT type variable (only static type)
Dim Byref As Object roo0 = o0    '' Object type reference, referring to an Object instance (static type + dynamic type)
Dim Byref As Object rou0 = u0    '' Object type reference, referring to a UDT instance (static type + dynamic type)
Dim Byref As Object rodu0 = du0  '' Object type reference, referring to a derivedUDT instance (static type + dynamic type)
Dim Byref As UDT ruu0 = u0       '' UDT type reference, referring to a UDT instance (static type + dynamic type)
Dim Byref As UDT rudu0 = du0     '' UDT type reference, referring to a derivedUDT instance (static type + dynamic type)

Print , "static-type", "dynamic-type"
Print "----------------------------------------"
Print "io", staticTypeName(i0)
Print "d0", staticTypeName(d0)
Print "s0", staticTypeName(s0)
Print "su0", staticTypeName(su0)
Print "o0", staticTypeName(o0), dynamicTypeName(o0)
Print "u0", staticTypeName(u0), dynamicTypeName(u0)
Print "roo0", staticTypeName(roo0), dynamicTypeName(roo0)
Print "rou0", staticTypeName(rou0), dynamicTypeName(rou0)
Print "rodu0", staticTypeName(rodu0), dynamicTypeName(rodu0)
Print "ruu0", staticTypeName(ruu0), dynamicTypeName(ruu0)
Print "rudu0", staticTypeName(rudu0), dynamicTypeName(rudu0)

Sleep

Code: Select all

              static-type   dynamic-type
----------------------------------------
io            Integer
d0            Double
s0            String
su0           simpleUDT
o0            Object        Object
u0            UDT           UDT
roo0          Object        Object
rou0          Object        UDT
rodu0         Object        derivedUDT
ruu0          UDT           UDT
rudu0         UDT           derivedUDT
Post Reply