Revision [21690]

This is an old revision of KeyPgTypeAlias made by fxm on 2016-11-09 15:47:15.

 

Type (Alias)


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:
symbol may refer to any declared data type including a built-in data type, Sub or Function pointer, Type declaration, Union declaration, or Enum declaration.

A type alias can be used to allow forward declarations of parameters in procedure declarations, but only used with pointers or parameters passed by reference (excluding arrays). Generally the bodies of such procedures need to be implemented further in the code once the actual types are well defined.
A type alias can also be used to allow forward declarations of data fields in User Defined Types, but only used with pointers.

A type alias must be used to allow declaration of pointer to a function pointer, otherwise ptr applies on return type and not on function.

Examples:
Type ParentFwd As Parent
Type Child
    Name As ZString * 32
    ParentRef As ParentFwd Ptr
    ''...
End Type

Type Parent
    Name As ZString * 32
    ChildList(0 To 9) As Child
    ''...
End Type

Dim p As Parent
p.Name = "Foo"
With p.ChildList(0)
    .Name = "Jr."
    .ParentRef = @p
    '' ...
End With   

With p.ChildList(0)
    Print .Name; " is child of "; .parentRef->Name
End With

Function triple (ByVal i As Integer) As Integer
    Return 3 * i
End Function

Type As Function (ByVal As Integer) As Integer function_alias

'Dim As Function (Byval As Integer) As Integer f = @triple  '' this syntax works but is less readable than the next line
Dim As function_alias f = @triple
Print f(123)

'Dim As Function (Byval As Integer) As Integer Ptr pf = @f  '' this syntax does not work because Ptr applies on Integer and not on function
Dim As function_alias Ptr pf = @f                           '' this syntax works
Print (*pf)(123)                                            '' the dereferenced pointer to procedure pointer must be surrounded by parenthesis

Differences from QB:
See also:
Back to User Defined Types
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki



sf.net phatcode