Type Aliases


Additional names for variable or object types

Overview
Declaration
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 "float", a procedure, and defines and initializes two variables of that type:

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:

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


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

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)


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:

Dim pf As Function() As Integer Ptr

Type 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

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.

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


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.

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 )



Back to Programmer's Guide


Valid XHTML :: Valid CSS: :: Powered by WikkaWiki



sf.net phatcode