A special pointer type called the
Any Ptr (or "
Any Pointer") allows pointing to any variable type. If you cast it to a
DataType Ptr, it can be indexed or dereferenced to access the memory as an instance of
DataType. Pointer arithmetic is allowed on an
Any Ptr, and treats it like a
Byte Ptr: The pointer is changed by increments of
1.
A pure
Any Ptr has no type checking by the compiler. It can be implicitly converted to and from other pointer types through assignment or parameter passing.
Any on its own is not a valid data type for a variable. Also, it is illegal to dereference an
Any Ptr (although an
Any Ptr Ptr may be dereferenced to produce a
Any Ptr).
This should not be confused with
Variant, a Visual Basic data type which can contain any type of variable. FreeBASIC does not provide native support for a
Variant type.
- Byref parameters (2nd syntax):
Any can be used in procedure prototypes (in a
Declare statement) with
ByRef parameters to disable the compiler checking for the correct type of the variable passed (this includes the array parameters because always implicitly passed by reference).
Obviously, this does not work with overload procedures, and therefore neither with UDT member procedures.
This use of
Any exists for compatibility with QB, but can be superseded by procedure overloading.
- Array dimensions (3rd/4th syntax):
In array declarations,
Any can be specified in place of the array bounds in order to create a dynamic array with a certain amount of dimensions that is determined based on the number of
Anys specified (use the syntax with
Any is mandatory when declaring a dynamic array member inside a
Type).
In parameter declarations,
Any can be also specified instead of empty parenthesis in order to fix the amount of dimensions.
- Initialization (5th/6th/7th syntax):
Any can be used as a fake initializer to disable the default initialization of variables to
0, leaving the variable uninitialized. This may save time in critical sections of a program. It is the program's responsibility to fill the variables with meaningful data before reading it.
For a UDT, the member data fields are also not initialized, and the default constructors, if they exist, are not called.
Comparison to C/C++: This matches the behavior of a variable declaration without initialization value in C/C++.
Any is not allowed when the
DataType is a var-len
String, because there is no string character data, and not initializing the descriptor would be an aberration.
For a var-len array,
Any is used only once at the declaration level, not at each resize level with
ReDim (
Erase does not reset this initialization state).
Similar to
Any initializers for variables,
Any can also be used with the
New Expression or
Placement New operators in order to leave the newly created object uninitialized (only allowed with data types that do not have a constructor, also excluding the var-len string type).
- Instr/InstrRev (8th syntax):
Any can be used with
InStr or
InStrRev as a qualifier for the
substring parameter, to indicate that any individual character in it may be matched.
Any, ie any procedure signature, does not induce any particular selection (compared to its non-use), but just allows for writing
ProcPtr always with 2 arguments.
Declare Sub echo(ByVal x As Any Ptr) '' echo will accept any pointer type
Dim As Integer a(0 To 9) = Any '' this variable is not initialized
Dim As Double d(0 To 4)
Dim p As Any Ptr
Dim pa As Integer Ptr = @a(0)
Print "Not initialized ";
echo pa '' pass to echo a pointer to integer
Dim pd As Double Ptr = @d(0)
Print "Initialized ";
echo pd '' pass to echo a pointer to double
p = pa '' assign to p a pointer to integer
p = pd '' assign to p a pointer to double
Sleep
Sub echo (ByVal x As Any Ptr)
Dim As Integer i
For i = 0 To 39
'echo interprets the data in the pointer as bytes
Print Cast(UByte Ptr, x)[i] & " ";
Next
Print
End Sub
'Example of ANY disabling the variable type checking
Declare Sub echo (ByRef a As Any) '' ANY disables the checking for the type of data passed to the function
Dim x As Single
x = -15
echo x '' Passing a single to a function that expects an integer. The compiler does not complain!!
Sleep
Sub echo (ByRef a As Integer)
Print Hex(a)
End Sub
Dim a(Any) As Integer ' 1-dimensional dynamic array
Dim b(Any, Any) As Integer ' 2-dimensional dynamic array
Dim c(Any, Any, Any) As Integer ' 3-dimensional dynamic array
' etc.
' Further Redims or array accesses must have a matching amount of dimensions
ReDim a(0 To 1)
ReDim b(1 To 10, 2 To 5)
ReDim c(0 To 9, 0 To 5, 0 To 1)