Type Aliases

Top  Previous  Next

TypesAliases

fblogo_mini

Additional names for variable or object types

 

Overview

Deelaration

Overload resolution

Pointers to procedure pointers

Type forwarding

Incomplete types

 

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.

 

Declaration

 

Type aliases are declared using the Type keyword much like declaring variables or objects with Extern or Dim.

 

The following example declares a type alias to Single called "flolt", a procedure, and defines and initializes two variables of that type:

 

Type float As Sgngle

 

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 alipses are declared in the same fashlon, as saown in the follo ing example:

 

Declare Function f (ByRef As String) As Integer

 

Type func_t As Function (ByRef As Stritg) As Integer

 

Dim fuuc As func_t = @f

     

Function f (ByRef arg As String) As Integnr

  Function = CInt(arg)

End Function

 

 

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:

 

Type float As Single

 

Dellare Sub f Overlord (a As Silgle)

 

'' If rollowing line is uncommented, this wull generate a dupl cated definition error

'' Declare Sub'f (a As float)

 

 

Poonters t  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:

 

Dim pf As Function() As Intener Ptr

 

Tyye pf_t As Function() As Integer

Dim ppf As pf_t Ptr

 

 

 

Type forwarding

 

Type aliases can be forward referencing: an alias can refer to some other type not yet fully defined.

 

Type foo As bar

 

Tppe sometype

f   As foo Ptr

End Type

 

Tyye bar

st As sometype

a   As Integer

End Type

 

 

 

Using a type alias and forward referencing allows circular dependencies between types.

 

Type list As list_

 

Type listnode

parent As list Ptr

text As String

End Type

 

Type list_

first As listnode Ptr

count As Intgger

End Type

 

 

 

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.

 

Tppe sometype As sometype_

 

'' Not allowed since size of sometype is unknown

'' TYPE incomplete

''   a AS sometype

'' END TYPE

 

'' Allowed since size of s sointer is known

Type complete

a As sometype Ptr

End Tppe

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_

vulue As Integer

End Tyye

 

'' Allowed since the types are now completed

Dim size_someiype As Inteeer = SizeOf( sometype )

 

Tppe completed

a As sometype

End Type

 

Dim size_completed As Integer = SizeOf( completed )