{{fbdoc item="title" value="Type Aliases"}}---- Additional names for variable or object types **{{anchor name="OVERVIEW|Overview"}}** **{{anchor name="DECL|Declaration"}}** **{{anchor name="OVERLOAD|Overload resolution"}}** **{{anchor name="PROCPTRPTR|Pointers to procedure pointers"}}** **{{anchor name="TYPEFORWARD|Type forwarding"}}** **{{anchor name="INCTYPES|Incomplete types"}}** {{anchor name="OVERVIEW"}}{{fbdoc item="section" value="Overview"}} Type aliases are alternative names for a type. They can be used to facilitate a mass change from one type to another, save typing, or make circular dependency possible. {{anchor name="DECL"}}{{fbdoc item="section" value="Declaration"}} Type aliases are declared using the ##[[KeyPgTypeAlias|Type]]## keyword much like declaring variables or objects with [[KeyPgExtern|Extern]] or [[KeyPgDim|Dim]]. The following example declares a type alias to ##[[KeyPgSingle|Single]]## called "##//float//##", a procedure, and defines and initializes two variables of that type: {{fbdoc item="filename" value="examples/manual/proguide/typalias/builtin.bas"}}%%(freebasic) type float as single declare function add (a as float, b as float) as float dim foo as float = 1.23 dim bar as float = -4.56 %% Procedure pointer type aliases are declared in the same fashion, as shown in the following example: {{fbdoc item="filename" value="examples/manual/proguide/typalias/procptr_1.bas"}}%%(freebasic) Declare Function f (ByRef As String) As Integer Type func_t As Function (ByRef As String) As Integer Dim func As func_t = @f Function f (Byref arg as String) as Integer Function = Cint(arg) End Function %% {{anchor name="OVERLOAD"}}{{fbdoc item="section" value="Overload resolution"}} Type aliases are just that - aliases. For all intents and purposes, a type alias **is** the type it aliases. So as far as procedure overload resolution is concerned, a procedure declared with a parameter of type "##//alias_to_T//##" is the same as a procedure declared with a parameter of type "##//T//##" (the same applies to overloading member procedures as well). In other words, it is an error - duplicated definition - to declare a procedure where parameters differ only in a type and its alias, as the following example shows: {{fbdoc item="filename" value="examples/manual/proguide/typalias/overload.bas"}}%%(freebasic) Type float As Single Declare Sub f Overload (a As Single) '' If following line is uncommented, this will generate a duplicated definition error '' Declare Sub f (a As float) %% {{anchor name="PROCPTRPTR"}}{{fbdoc item="section" value="Pointers to procedure pointers"}} Pointers to procedure pointers are just like any other pointer type, except they point to procedure pointers. Because the syntax for declaring procedure pointers doesn't allow directly creating a pointer to procedure pointer when the procedure is a function (because ptr applies on return type and not on procedure), a type alias is used. The following example declares a pointer to a procedure returning an integer pointer, and then a pointer to a pointer to a procedure returning an integer: {{fbdoc item="filename" value="examples/manual/proguide/typalias/procptr_2.bas"}}%%(freebasic) dim pf as function() as integer ptr type pf_t as function() as integer dim ppf as pf_t ptr %% {{anchor name="TYPEFORWARD"}}{{fbdoc item="section" value="Type forwarding"}} Type aliases can be forward referencing: an alias can refer to some other type not yet fully defined. {{fbdoc item="filename" value="examples/manual/proguide/typalias/fwd_1.bas"}}%%(freebasic) TYPE foo AS bar TYPE sometype f AS foo PTR END TYPE TYPE bar st AS sometype a AS INTEGER END TYPE %% Using a type alias and forward referencing allows circular dependencies between types. {{fbdoc item="filename" value="examples/manual/proguide/typalias/fwd_2.bas"}}%%(freebasic) TYPE list as list_ TYPE listnode parent AS list PTR text AS string END TYPE TYPE list_ first AS listnode PTR count AS integer END TYPE %% {{anchor name="INCTYPES"}}{{fbdoc item="section" value="Incomplete types"}} A type is considered incomplete until the size of it, that is the number of bytes it would need to occupy in memory is known, and the offsets of all of its fields are known. It is not possible to allocate space for an incomplete type. It is not possible to declare a variable having the data type of an incomplete type, pass an incomplete type as a parameter, or access the members of an incomplete type. However, pointers to incomplete types may be allocated, declared as members in other types, or passed as parameters to a procedures since the size of a pointer is known. {{fbdoc item="filename" value="examples/manual/proguide/typalias/incomp.bas"}}%%(freebasic) TYPE sometype AS sometype_ '' Not allowed since size of sometype is unknown '' TYPE incomplete '' a AS sometype '' END TYPE '' Allowed since size of a pointer is known TYPE complete a AS sometype ptr END TYPE DIM x AS complete '' Not allowed since size of sometype is still unknown '' DIM size_sometype AS INTEGER = SIZEOF( sometype ) '' Complete the type TYPE sometype_ value as integer END TYPE '' Allowed since the types are now completed DIM size_sometype AS INTEGER = SIZEOF( sometype ) TYPE completed a AS sometype END TYPE DIM size_completed AS INTEGER = SIZEOF( completed ) %% {{fbdoc item="back" value="CatPgProgrammer|Programmer's Guide"}}