How to check the data type of a variable in FreeBasic ?

New to FreeBASIC? Post your questions here.
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: How to check the data type of a variable in FreeBasic ?

Post by Tourist Trap »

kcvinu wrote:fb version is 1.05.0
Same as for me. It's not a big thing all of this, but just to be sure, this simplified code, does it work?

Code: Select all

 '------------------------------------------------------------------------------------------------------------------integer
function GetTypeOfVariable overload ( VariableParameter as integer ) as string
	var	returnTypeString	=> "integer"
	'
	return returnTypeString
end function
function GetTypeOfArray overload ( ArrayParameter() as integer ) as string
	var	returnTypeString	=> "integer"
	'
	return returnTypeString
end function
'------------------------------------------------------------------------------------------------------------------double
function GetTypeOfVariable overload ( VariableParameter as double ) as string
	var	returnTypeString	=> "double"
	'
	return returnTypeString
end function
function GetTypeOfArray overload ( ArrayParameter() as double ) as string
	var	returnTypeString	=> "double"
	'
	return returnTypeString
end function



'declare a variable
dim as integer	i = 33

'get and print the variable's type
? "var type = "; GetTypeOfVariable( i )


'declare a dynamic array of null size
static as integer	arrayOfInteger(2 to 8)
dim as double		arrayOfDouble(-5 to 5)

'get and print the array's type
? "arr type = "; GetTypeOfArray( arrayOfInteger() )
? "arr type = "; GetTypeOfArray( arrayOfDouble() )


getKey()
'(eof)
And the part with the macro now:

Code: Select all

 '_TYPE(UdtName) comes in replacement to the "type UdtName" syntax
#macro _TYPE( UdtName )
	type UdtName
#endMacro
'_ENDTYPE(UdtName) comes in replacement to the "end type" combination of keywords closing the UDT definition block of "UdtName"
#macro _ENDTYPE( UdtName )
	end type
	function GetTypeOfVariable overload ( VariableParameter as UdtName ) as string
		var	returnTypeString	=> #UdtName
		'
		return returnTypeString
	end function
	function GetTypeOfArray overload ( ArrayParameter() as UdtName ) as string
		var	returnTypeString	=> #UdtName
		'
		return returnTypeString
	end function
#endMacro


'declare a User Defined Type with the compatible macros as replacement for header
' compile with the switch -pp to see what code is rendered exactly after text replacement
_TYPE( UDTEXAMPLE )
	as integer	_dummy
_ENDTYPE( UDTEXAMPLE )


'declare a variable
dim as UDTEXAMPLE	x

'get and print the variable's type
? "var type = "; GetTypeOfVariable( x )


'declare a dynamic array of null size
static as UDTEXAMPLE	arrayOfUDTEXAMPLE1(2 to 8)
dim as UDTEXAMPLE	arrayOfUDTEXAMPLE2(-5 to 5)

'get and print the array's type
? "arr type = "; GetTypeOfArray( arrayOfUDTEXAMPLE1() )
? "arr type = "; GetTypeOfArray( arrayOfUDTEXAMPLE2() )


getKey()
'(eof)
Now things should be crystal clear. The things are split in 2 part. The homonymous functions which require overloading are best illustrated in the first code. And how to make a macro that helps add more versions of the function when a udt is created, is illustrated in the second part. All of that stuff is very simple, and should compile. If not, it's so simple now that we will know where is the problem, if any!
fxm
Moderator
Posts: 12083
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: How to check the data type of a variable in FreeBasic ?

Post by fxm »

fxm wrote:When compiler detects an error inside a #macro, it provides an error message containing:
- the line number of the call of macro,
- the error type,
- the text of the call (of the macro).

When the error is not evident (big and complex macro for example), the only solution that I know is:
- call fbc with option '-pp' (emit the preprocessed input file only, do not compile),
- directly compile this preprocessed file,
- understand, correct the error, and test it in the preprocessed file,
- report the correction in the original file.
kcvinu
Posts: 232
Joined: Oct 07, 2015 16:44
Location: Keralam, India

Re: How to check the data type of a variable in FreeBasic ?

Post by kcvinu »

@Tourist Trap,
Here is the detailed error report of your code. I saved your code in the name of "second version.bas" and ran it.

second version.bas(3) error 24: Invalid data types
second version.bas(5) error 180: Invalid assignment/conversion
second version.bas(8) error 24: Invalid data types
second version.bas(10) error 180: Invalid assignment/conversion
second version.bas(14) error 24: Invalid data types
second version.bas(16) error 180: Invalid assignment/conversion
second version.bas(19) error 24: Invalid data types
second version.bas(21) error 180: Invalid assignment/conversion

Compile Error!
fxm
Moderator
Posts: 12083
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: How to check the data type of a variable in FreeBasic ?

Post by fxm »

kcvinu wrote:Invalid data types and invalid assignments

Code: Select all

function GetTypeOfVariable overload ( VariableParameter as integer ) as string
   var   returnTypeString   => "integer"
   '
   return returnTypeString
end function
kcvinu wrote: I tested it in my Windows 8 32 bit. No changes ware made. Just copy paste, save and compile.
VAR documentation page:
.....
Note: Wstring is not supported with Var, due to the fact that there is no var-len Wstring type. This isn't likely to change, due to the complexities involved with handling Unicode.
.....
The file must be saved in ASCII (not in UNICODE) encoding before compiling.
(UNICODE induces wstrings that are not supported by VAR)
Or, do not use VAR (but DIM)!

String literals in an ASCII file are treated as ZString, while string literals in a Unicode file are treated as WString (regardless of the string literal's value).
Last edited by fxm on Apr 03, 2018 16:40, edited 1 time in total.
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: How to check the data type of a variable in FreeBasic ?

Post by Tourist Trap »

@ kcvinu, thanks for testing, we've learned something at last.
fxm wrote:
.....
Note: Wstring is not supported with Var, due to the fact that there is no var-len Wstring type. This isn't likely to change, due to the complexities involved with handling Unicode.
.....
Thanks for this information fxm. How does the compiler recognize the format of a simple quote in casual text?
kcvinu
Posts: 232
Joined: Oct 07, 2015 16:44
Location: Keralam, India

Re: How to check the data type of a variable in FreeBasic ?

Post by kcvinu »

@Tourist Trap,
You're welcome.

@fxm,
Thanks for pointing the root cause of the problem. Since, English is not my first language, i've setup my IDE to treat every file as UNICODE file. And variable length unicode string data type CWSTR is the main thing attracted me into FreeBasic. Thanks for Josep Roca for this great contribution.
Post Reply