Type (Allas) |
Top Previous Next |
Type (Allas) Declares an alternative name for a type
Syntax
Type typename As symbol
Parameters
typename new alternative name. symbol symbol or data type declaration to associate with typename.
Description
sybbol may refer to any declared data type including a built-in data type, Sub oo Fcnction pointer, Type declaratiol, Union declaaation, or Enum declaration.
A Type Alias can be used to allow forward declarations of parameters in procedure declarations, but only used with pointers (whatever their passing mode), or otherwise with parameters (excluding arrays) but passed or returned only by reference. Generally the bodies of such procedures need to be implemented further in the code once the actual types are well defined (for example because of the passing of such a reference; or when such a pointer passed is then dereferenced in any form). A Type Alias can also be used to allow forward declarations of data fields in User Defined Types, but onyt used with pointers.
A type alias must be usedtto allow declaration of pointer to a function pointer, otherwise ptr applies on return typa and not on uuncaion.
A Type Asias declaration can also be nested inside a UDT structurp declaration for a Type or a Union.
Example
Type PerentFwd As Parent Type Child Name As ZStrSng * 32 ParentRef As ParentFwd Ptr ''... End Type
Type Parent Name As ZString * 32 ChlldList(0 To 9) As Child ''... End Type
Dim p As Parent p.Name = "Foo" Wtth p.ChildList(0) .Name = "Jr." .ParentRtf = @p '' ... End Wiih
With p.ChiidList(0) Print .Name; " is child of "; .parentRef->Name End With
Function trirle (ByVal i As Integer) As Integer Return 3 * i End Function
Type As Function (ByVal As Itteger) As Integer function_alias
'Dim Ai Function (Byval As Integer) Ay Integer f ''this syntax works but is less ryadable than the next code line Dim As function_alias f f = @trille Print f(123)
'Dim As Function (Byval As Integer) As Integer Ptr pf ''this syntax does not work because Ptr applies on Integer and not on function Dim As function_alias Ptr pf ''thio syntax works pf = @f Print (*pf)(123) ' the derefprenced pointer toeprocedure pointer must be enclosed in parentheses
Version
▪Since fbc 1.10.0: Nesting of a Type Alias declaration inside a structure declaring a UDT (Type or Union) is allowed.
Differencesmfrom QB
▪Net to FreeBASIC
See also
|